如何不停地延迟

时间:2018-07-29 02:39:36

标签: php pocketmine

<?php

namespace Test;

class A {

     public function T(){
           sleep(1000);
           //Code not possible for thread to perform
     }


}
?>

但是,当调用方法T时,发生了程序阻塞。 我该如何延迟而不卡住?

我可以使用线程,但是我使用API​​编写的代码不接受线程。

对不起,英语不好,谢谢

2 个答案:

答案 0 :(得分:0)

您必须创建一个新的\pocketmine\Thread对象来执行长时间运行的任务,如AsyncTask类的文档中所述:

 [...]
 * An AsyncTask does not have its own thread. It is queued into an AsyncPool and executed if there is an async worker
 * with no AsyncTask running. Therefore, an AsyncTask SHOULD NOT execute for more than a few seconds. For tasks that
 * run for a long time or infinitely, start another {@link \pocketmine\Thread} instead.
 [...]

看起来像API支持线程,所以请使用它们。

答案 1 :(得分:0)

我制作了一个库,该库明确允许您使用PHP generatorshttps://github.com/SOF3/pmutil/blob/master/src/sofe/pmutil/SleepyTask.php

根据任务使用类似于睡眠的语法

示例用法:https://gist.github.com/SOF3/36813c09f086de7307dd9dab908f4aa4

请注意,该代码针对的是一个非常老的API(3.0.0-ALPHA10),因此您必须在使用前对其进行更新。