-2
<?php

namespace Test;

class A {

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


}
?>

But, When call method T, Program Stucking occured. how can i delay without stucking?

i can use thread but, the code that I wrote with using API doesn't accept Thread.

sorry for bad English, and thanks

SOFe
  • 7,867
  • 4
  • 33
  • 61
  • 1
    You're explaining too little about the context **and** purpose for the intended pause. Generally you want to introduce artificial delays client-side (JavaScript), not server-side (PHP). – mario Jul 29 '18 at 02:58
  • 1
    I'm not sure what are you trying to do. If you want to wait, it is waiting. If you don't want to wait, just remove that `sleep` command. – user202729 Jul 29 '18 at 06:37
  • @mario It is not Web PHP, game's additional plugin written in PHP. – Verify System Email Jul 29 '18 at 06:44
  • @user202729 I want to wait without stucking – Verify System Email Jul 29 '18 at 06:45
  • @VerifySystemEmail Why do you want to wait but not wait at the same time? What "games' additional plugin" are you talking about? What are you really trying to do? What API can you use? – Progman Jul 29 '18 at 08:14
  • @Progman I'm making MCPE's third party, called PocketMine-MP's plugin. but my server accomplish over 800 users so Server must not stuck when method called. (github.com/pmmp/PocketMine-MP see their AsyncTask) and what i want to make is the plugin that fireworks fly around the players. (i already wrote the code about firworks but, when use them in Async, it crashed (can use them in main thread, but use them in main thread, CPU, Ram usage becomes very high). – Verify System Email Jul 29 '18 at 08:59

2 Answers2

0

You have to create a new \pocketmine\Thread object for long running task, as mentioned in the documentation of the AsyncTask class:

 [...]
 * 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.
 [...]

So it looks like the API support threads, so use them.

Progman
  • 16,827
  • 6
  • 33
  • 48
  • nope. when i use Player class or Server class in AsyncTask, this error occured. `BadMethodCallException: "Cannot serialize Server instance"` – Verify System Email Jul 29 '18 at 13:50
  • @VerifySystemEmail Please check https://forums.pmmp.io/threads/cannot-serialize-server-instance-when-i-schedule-an-asynctask.4364/ and/or https://forums.pmmp.io/threads/server-instance-asynctask.5225/ on how to work with `Server` and `Player` instances in your task. – Progman Jul 29 '18 at 13:54
  • @VerifySystemEmail The API support threads, it defines even a class to use for long running task (namely the `\pocketmine\Thread` class). But that you cannot hold a reference of the `Server` instance in the thread object is a completely different problem. – Progman Jul 29 '18 at 14:33
  • i understood what you said. but what i want to make is using Server and Player class but can't use in AsyncTask so i said its not supported. good, so i can't do that. right? – Verify System Email Jul 30 '18 at 00:11
  • @VerifySystemEmail You can use the `Server` and `Player` classes, but you cannot save them in fields of your new class because they cannot be serialized (which is required for your class). You might want to ask further questions about PocketMine with the authors/community of PocketMine at https://forums.pocketmine.net/forums/development/. – Progman Jul 30 '18 at 14:39
  • @VerifySystemEmail Also, you can use `storeLocal()` and `fetchLocal()` as written in https://github.com/pmmp/PocketMine-MP/blob/master/src/pocketmine/scheduler/AsyncTask.php. – Progman Jul 30 '18 at 14:40
0

I have made a library that explicitly allows you to use a sleep-alike syntax based on tasks, using PHP generators: https://github.com/SOF3/pmutil/blob/master/src/sofe/pmutil/SleepyTask.php

Example use: https://gist.github.com/SOF3/36813c09f086de7307dd9dab908f4aa4

Note that the code targets a really old API (3.0.0-ALPHA10), so you have to update it before using.

SOFe
  • 7,867
  • 4
  • 33
  • 61