I've created an Async extended class to be called by scheduleAsyncTask method. But on the onRun() method i can't retreive any object from my plugin ? Even if I give the plugin reference in the contructor. Even static method are not found and i get an autoload error. Is it normal ?
Yeah, that wouldn't be thread safe Pass them in the constructor to the AsyncTask and then setResult the output.
Ok i understand, but is there really no treadsafe methode in the server api ? Because i need to do a large number of getBlock in the main level. (To scan a part of the map) And i want to do it async to have no impact on the main thread. I cannot either log anything with the logger ? What is the best way to do it ? Is it possible to pass the level to the constructor ? I've done it but it seems to be not allowed to use it after in the OnRun()
Also, don't call API functions from other threads. It is said on the documentation comment, and Level::getBlock() is an API function.
Ok that's clear No API Call from the OnRun() I will do all my getBlock() in the main thread. But limit the number in each call. thx
But look at this (from the pocketmine source code) PHP: namespace pocketmine\scheduler;use pocketmine\Server;use pocketmine\utils\Utils;class SendUsageTask extends AsyncTask{ public $endpoint; public $data; public function __construct($endpoint, array $data){ $this->endpoint = $endpoint; $this->data = serialize($data); } public function onRun(){ Utils::postURL($this->endpoint, unserialize($this->data)); } public function onCompletion(Server $server){ }} The Utils:ostURL() is in the API ? Why this one is allowed ?
Not exactly. What he meant about the API is, usually API calls would just add delayed operations or pass objects. The above utils call only calls a function that is in fact a shortcut for several PHP native functions.
This is the time when we come to think this: First, the function should not refer to any static properties in other threads, including the main thread. Second, the function should only call [other functions that ultimately only call] native PHP functions.
I'm trying to finish my plugin, but i have issues... How to debug the onRun() method while I can't call logger method (because it's API method) ? I've tried to write on a file, but i've a segmentation fault without any debug trace :/ What is the purpose of the setResult / getResult / HasResult method ? Is it the only was to return a result from the onRun() method ans read it on the OnCompletion() method ?
Try using print(), you should be able to write to files without issues (I have used that to debug). Set result is so the task can output something to be collected by the plugin (if a result is set the task won't be removed from memory until getResult() is called). Just read AsyncTask.php, it's pretty straightforward.
print() is ok thx ! My plugin is running now. But, I have a last question : Why do we need to serialize some parameters Here (sendUsage.php), $data is serialized, while $endpoint is not ? Is it correct to pass plugin instance to the contructor and to use it on the OnCompletion() methode (which is executed in the main thread) ? PHP: namespace pocketmine\scheduler;use pocketmine\Server;use pocketmine\utils\Utils;class SendUsageTask extends AsyncTask{ public $endpoint; public $data; public function __construct($endpoint, array $data){ $this->endpoint = $endpoint; $this->data = serialize($data); } public function onRun(){ Utils::postURL($this->endpoint, unserialize($this->data)); } public function onCompletion(Server $server){ }}