在线程中存储闭包失败 [英] Storing closures in thread fails

查看:55
本文介绍了在线程中存储闭包失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在线程运行完成后使用闭包作为回调.但是,我遇到了 PHP 或 pthread 扩展的限制/失败问题.

I'm trying to use a closure as a callback once a thread is done running. However I'm running into what seems to be a limit/failure of PHP or the pthread extension.

我的开发堆栈在 Win7 x64 上运行,PHP 5.5.3 x86 TS,pthread 版本 0.44.

My dev stack is running on Win7 x64 with PHP 5.5.3 x86 TS, pthread version 0.44.

以下代码有效:

class Test
{
    public $callbackVar;
}

$test = new Test();

$callbackVar = function()
{
    echo "Callback var invoked.";
};

$test->callbackVar = $callbackVar;
$test->callbackVar->__invoke();

但是一旦我从线程派生测试,运行脚本就会出错 :

class Test extends Thread
{
    public $callbackVar;
    public function run() { }
}

$test = new Test();

$callbackVar = function()
{
    echo "Callback var invoked.";
};

$test->callbackVar = $callbackVar;
// assert() returns true
assert($test->callbackVar === null);
$test->callbackVar->__invoke();   

使用以下输出

Fatal error: Call to a member function __invoke() on a non-object

有人遇到过这个问题吗?任何可能的解决方法?如果可能的话,我宁愿不使用 eval...我已经尝试了很多解决方法,例如重新包装到另一个闭包中,使用 ReflectionFunction,...没有任何效果.

Anyone ever had this issue ? Any possible workaround ? I'd rather not use eval if possible... I've tried many workarounds, such as rewrapping into another closure, using a ReflectionFunction, ... nothing cuts it.

推荐答案

Zend 不允许您序列化闭包对象.

Zend does not allow you to serialize closure objects.

因此这不是您应该尝试解决的问题,可能在未来某个时候 Zend 将允许对闭包进行序列化,届时 pthread 将不需要更改.

So it's not something you should try to work around, possibly at some time in the future Zend will allow serialization of Closures, pthreads will not require changes at that time.

你只需要按照老式的方式来做......

You'll just have to do it the old fashioned way ...

这篇关于在线程中存储闭包失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆