-
Identify the ObjectScript code that you want to process in parallel. See Code Requirements.
-
Divide your code into units of work.
-
Create a work queue, which is an instance of the %SYSTEM.WorkMgrOpens in a new tab class. To do so, call the %New() method of the %SYSTEM.WorkMgrOpens in a new tab class. The method returns a work queue.
You can specify the number of parallel worker jobs to use, or you can use the default, which depends on your machine and operating system. Additionally, if you have created categories, you can specify the category that the jobs should be taken from.
When you create a work queue, the Work Queue Manager creates the following artifacts:
-
A global that contains information about the work queue such as what namespace the work queue runs in
-
A location and an event queue for the serialized units of work that the work queue must process
-
A location and an event queue for completion events that are created as the work queue finishes processing units of work
-
Add units of work (also called work items) to the work queue. To do so, you can call the Queue() or QueueCallback() method. As arguments, you pass the name of a class method (or subroutine) and any corresponding arguments.
Processing begins immediately on items added to the queue.
If there are more items in the queue than there are worker jobs available to the queue, then the jobs compete to empty the queue. For example, if there are 100 items and four jobs, each job removes an item from the head of the queue, processes it, and then returns to the head of the queue to remove and process another item. This pattern continues until the queue is empty.
The Work Queue Manager uses the security context of the caller when running a work item.
When you queue work items, the Work Queue Manager performs the following tasks:
-
Serializes the arguments, security context, and class method or subroutine that comprises the unit of work, and then inserts the serialized data into the global that lists the units of work associated with the work queue.
-
Signals an event on the work queue.
-
If additional worker jobs are required and available to process the units of work, causes a worker job to attach to the work queue and decrements the number of available worker jobs.
-
Wait for the work to be completed. To do so, you can call the WaitForComplete() method of the work queue.
The Work Queue Manager then performs the following tasks:
-
Waits for a completion event
-
Displays output such as workload metrics to the terminal
-
Collects any errors related to the unit of work
-
If you added units of work to the work queue using the QueueCallback() method, runs the callback code
-
Continue processing as appropriate for your application.
Example
The following example shows these basic steps:
Set queue=##class(%SYSTEM.WorkMgr).%New()
For i = 1:1:filelist.Count() {
Set sc=queue.Queue("..Load",filelist.GetAt(i))
If $$$ISERR(sc) {
Return sc
}
}
Set sc=queue.WaitForComplete()
If $$$ISERR(sc) {
Return sc
}
The code initializes the Work Queue Manager and then iterates through a list of files. For each file, the code adds a work queue item that loads a file. After adding all the work queue items, the code waits for the work to be completed.