2 <h2> Multi-Tasking and Multi-Threading Support</h2>
4 To perform parallel execution in PHP without forking a new process, you may
5 take advantage of one of these new facilities:
9 The pagelet server is similar to a CURL call to localhost. Look for "Pagelet
10 Server" in compiled program's options for how to set it up. The new pagelet
11 server functions work like this:
13 // This starts a pagelet server thread to process the URL just as if
14 // it's a new web request with specified headers and post data.
15 // The request method would be GET if the post data is empty.
16 $task = <b>pagelet_server_task_start</b>($url, $headers, $post_data);
17 // Main thread can now do extra work while pagelet server is processing.
19 // Optionally make this non-blocking call any time to check status.
20 $status = <b>pagelet_server_task_status</b>($task);
22 // Finally, we make a blocking call to wait for pagelet server's result,
23 // which is the entire output of the web page, with response headers and
24 // status code. The status code is set to 0 if a flushed partial response is
25 // successfully returned and the pagelet server is still active.
27 // A timeout of 0 is interpreted as an infinite timeout. The status code is
28 // set to -1 in the event of a timeout.
29 $headers = array(); $code = 0;
30 $result = <b>pagelet_server_task_result</b>($task, $headers, $code,
35 The xbox task system is designed to provide cross-box messaging as described in
36 "server.xbox_server" documentation. The xbox task system may also be used to
37 execute a task on the local machine in a separate thread. Here is an example:
39 // We start an xbox task by sending to localhost a message.
40 $task = <b>xbox_task_start</b>($message);
41 // Main thread can now do extra work while xbox task is processing.
43 // Optionally make this non-blocking call any time to check status.
44 $status = <b>xbox_task_status</b>($task);
46 // Finally, we make a blocking call to check message processing returns.
48 $code = <b>xbox_task_result</b>($task, $timeout_ms, $ret);
50 On the message processing side, one has to implement a PHP function like this:
52 function <b>xbox_process_message</b>($msg) {
57 Note that an xbox thread starts its execution with its own global state without
58 sharing anything with main thread, other than $msg and $ret that are passed
59 between these threads at enter and exit points.