frankenphp icon indicating copy to clipboard operation
frankenphp copied to clipboard

feat: task workers

Open AlliBalliBaba opened this issue 3 months ago • 19 comments

This PR adds a new type of worker thread 'task workers'

caddyfile:

frankenphp {
    task_worker path/to/worker/file.php 10 # same config as regular workers
}

from a regular or worker PHP thread:

frankenphp_handle_task('my task'); # send a task

inside of the task worker:

# equivalent to frankenphp_handle_request(), but handling tasks instead
while(frankenphp_handle_task(string $task) { 
    echo $task; // output: 'my task'
}

Allows:

  • Offloading work to a separate threadpool that is started at Init()
  • Just running any script in a loop (even without calling frankenphp_handle_task)
  • Interacting with threads from go via extensions (#1795)

#1795 is also the reason why this feature is marked as experimental for now. Currently tasks are only passed as strings, but it would theoretically be possible to pass anything, even closures (while keeping thread safety in mind).

AlliBalliBaba avatar Sep 17 '25 20:09 AlliBalliBaba