Is there a way to pass service/containers in child process? Is there a way to pass service/containers in child process? symfony symfony

Is there a way to pass service/containers in child process?


Short answer: no

Longer answer: Spatie Async serializes task before adding it to the pool, so you might need an alternative solution.

Why Async needs a serialized task

See relevant code to understand what's going on:Pool::add > ParentRuntime::createProcess > ParentRuntime::encodeTask.

For more discussion, see this or this issue in spatie/async's issue list.

Alternative: Symfony Messenger

There are many alternatives to this problem. Since you're using Symfony, you might be interested in Symfony Messenger to send messages to a handler. Those handlers can use dependency injection:

class DefaultController extends AbstractController{    public function index(MessageBusInterface $bus)    {        $bus->dispatch(new ServiceTask('Look! I created a message!'));    }}class ServiceTaskHandler implements MessageHandlerInterface{    protected $accountService;    protected $serviceFactory;    public function __construct(ServiceFactory $serviceFactory)    {        $this->serviceFactory = $serviceFactory;        $this->accountService = $this->serviceFactory->getAccountService();    }    public function __invoke(ServiceTask $task)    {         $this->accountService->handle($task);                }}

Be aware that a Task (ServiceTask in this example) should (like spatie/async's task) be serializable as well. So you might send an ID as a message, and look up that ID in your MessageHandler


Serialization of 'Closure' is not allowed is an error that is formed when passing a cued event as an argument. Likely the issue has to do with creating the object in a factory then passing it as part of the constructor.