Loading TOC...


   $function as function() as item()*,
   [$options as (element()|map:map)?]
) as item()*


Place the specified function value on the task queue for evaluation.

$function A zero arity function value to execute.
$options The options node. The default value is (). The node must be in the xdmp:eval namespace. For detailed options information, see xdmp:spawn.

Required Privileges


Usage Notes

The xdmp:spawn-function function places the specified function value in the task queue to be processed. The function will be executed when the task server has the available resources to process it. The tasks are processed in the order in which they are added to the queue.

Once xdmp:spawn-function is called, it cannot be rolled back, even if the transaction from which it is called does not complete. Therefore, use care calling xdmp:spawn-function from an update transaction. Once a module is spawned, its evaluation is asynchronous of the transaction in which xdmp:spawn-function was called. Consequently, if you call xdmp:spawn-function from a module, and if the module ends up retrying (for example, if a deadlock is detected), then the entire module is re-evaluated and the xdmp:spawn-function call is therefore called again. For details on how transactions work in MarkLogic Server, see "Understanding Transactions in MarkLogic Server" in the Application Developer's Guide.

If your function performs an update, then by default it will run in update transaction-mode, which runs as a multi-statement transaction. You must put an explicit xdmp:commit() with a transaction mode of update (otherwise it will automatically roll back). For implicit commits, specify a transaction-mode of update-auto-commit.


  let $message := "Hi!"
    xdmp:spawn-function(function() { xdmp:sleep(1000), xdmp:log($message) })
  => Puts the inline function in the task server queue.


for $x in (1 to 10)
xdmp:spawn-function(function() {xdmp:document-insert(
  fn:concat("/doc", $x, ".xml"), <foo>{$x * 2}</foo>)}, 
<options xmlns="xdmp:eval">

(: spawns functions to create the specified documents on the task server :) 

Stack Overflow iconStack Overflow: Get the most useful answers to questions from the MarkLogic community, or ask your own question.


The commenting feature on this page is enabled by a third party. Comments posted to this page are publicly visible.
  • Hi. It seems `xdmp.spawnFunction` is not available in JavaScript. Did I miss it? If it is not available, is there any idiomatic replacement? I can think of `xdmp:spawn-function` in conjunction with `xdmp:javascript-eval`, but I am interested if there is any alternative.
    • Obviously, an alternative is to use `xdmp.spawn`, using a JavaScript main module instead of a function then (not able to capture anything from the closure then...)
  • Note that if your function does database updates you need to both add an xdmp:commit() to the function AND specify an options node that lets spawn-function know that the function contains update code, the XQuery evaluator will not necessarily figure out that the function needs to run in update mode. Example: xdmp:spawn-function( function() { xdmp:document-insert( "/test/123.xml" , <test/> ) , xdmp:commit() } , <options xmlns="xdmp:eval"> <transaction-mode>update</transaction-mode> </options> )