-
Notifications
You must be signed in to change notification settings - Fork 503
Serialization of 'Closure' is not allowed in Worker #942
Comments
also happened in CentOS 7 with remi-php7.2 rpm packages. Installed Packages |
The properties of the instances that inherit from Threaded are serialized so that the information they host can be passed between contexts. When you pass your http client to your worker, the engine tries to serialize the instance of $result = serialize(function () {}); That will throw a fatal error: If you want to create an instance that is shared by all the threads of your worker you can do the following. <?php
use Worker;
class TaskQueueWorker extends Worker
{
protected static $client;
public function run()
{
// It is necessary to call the autoload composer file, since the context
// of the worker is new and does not have the spl autoload function
// for psr-4
require 'vendor/autoload.php';
}
public function getClient()
{
if (self::$client) {
return self::$client;
}
self::$client = new GuzzleHttp\Client();
return self::$client;
}
} You can learn more about how to share objects in the examples: |
@AlexSayHello That isn't sharing. Statics are thread-local, not shared. |
@dktapps When I refer to "shared" I mean that the instance is available from the thread through |
Does this means I must create the instance as a thread-local object if it uses an anonymous function or closure? I cannot control the code from others' package. |
Environment
PHP 7.2.19 (cli) (built: May 29 2019 14:17:01) ( ZTS MSVC15 (Visual C++ 2017) x86 )
Pthread 3.2.0
Win10 x64
Summary
When I assign a object with a member of GuzzleHttp\Client to a field of my worker class, I raise an exception saying Serialization of 'Closure' is not allowed.
Reproducing Code
class TaskQueueWorker extends \Worker
{
public function __construct($client)
{
$this->client = $client;
}
}
$client is an object of a class with a sub object of GuzzleHttp\Client.
Its package information:
"name": "guzzlehttp/guzzle",
"version": "6.3.3",
"source": {
"type": "git",
"url": "https://github.com/guzzle/guzzle.git",
"reference": "407b0cb880ace85c9b63c5f9551db498cb2d50ba"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/guzzle/guzzle/zipball/407b0cb880ace85c9b63c5f9551db498cb2d50ba",
"reference": "407b0cb880ace85c9b63c5f9551db498cb2d50ba",
"shasum": ""
},
"require": {
"guzzlehttp/promises": "^1.0",
"guzzlehttp/psr7": "^1.4",
"php": ">=5.5"
},
Expected Output
Actual Output
raise a fatal error.
The text was updated successfully, but these errors were encountered: