PocketMine-MP 5.15.1 git-be6754494fdbbb9dd57c058ba0e33a4a78c4581f
pocketmine\scheduler\AsyncTask Class Reference
+ Inheritance diagram for pocketmine\scheduler\AsyncTask:
+ Collaboration diagram for pocketmine\scheduler\AsyncTask:

Public Member Functions

 cancelRun ()
 
 checkProgressUpdates ()
 
 getResult ()
 
 hasCancelledRun ()
 
 hasResult ()
 
 isCrashed ()
 
 isFinished ()
 
 isSubmitted ()
 
 onCompletion ()
 
 onError ()
 
 onProgressUpdate ($progress)
 
 onRun ()
 
 publishProgress (mixed $progress)
 
 run ()
 
 setResult (mixed $result)
 
 setSubmitted ()
 

Protected Member Functions

 fetchLocal (string $key)
 
 reallyDestruct ()
 
 storeLocal (string $key, mixed $complexData)
 

Detailed Description

Class used to run async tasks in other threads.

An AsyncTask is run by a thread pool of reusable threads, and doesn't have its own dedicated thread. A thread is usually chosen from the pool at random to run the task (though a specific thread in the pool may be selected manually, if needed). Reusing threads this way has a much lower performance cost than starting an entirely new thread for every operation. AsyncTasks are therefore suitable for brief CPU-bound tasks, such as world generation, compression/decompression of data, etc.

AsyncTask SHOULD NOT be used for I/O-bound tasks, such as network I/O, file I/O, database I/O, etc. The server's central AsyncPool is used for things like compressing network packets for sending, so using AsyncTask for I/O will slow the whole server down, stall chunk loading, etc.

An AsyncTask SHOULD NOT run for more than a few seconds. For tasks that run for a long time or indefinitely, create a dedicated thread instead.

The Server instance is not accessible inside AsyncTask::onRun(). It can only be accessed in the main server thread, e.g. during AsyncTask::onCompletion() or AsyncTask::onProgressUpdate(). This means that whatever you do in onRun() must be able to work without the Server instance.

If you want to store non-thread-safe objects to access when the task completes, store them using AsyncTask::storeLocal.

Definition at line 61 of file AsyncTask.php.

Constructor & Destructor Documentation

◆ __destruct()

pocketmine\scheduler\AsyncTask::__destruct ( )
final

Definition at line 237 of file AsyncTask.php.

Member Function Documentation

◆ cancelRun()

pocketmine\scheduler\AsyncTask::cancelRun ( )
Deprecated:

Definition at line 123 of file AsyncTask.php.

◆ checkProgressUpdates()

pocketmine\scheduler\AsyncTask::checkProgressUpdates ( )

Definition at line 172 of file AsyncTask.php.

◆ fetchLocal()

pocketmine\scheduler\AsyncTask::fetchLocal ( string  $key)
protected

Retrieves data stored in thread-local storage.

If you used storeLocal(), you can use this on the same thread to fetch data stored. This should be used during onProgressUpdate() and onCompletion() to fetch thread-local data stored on the parent thread.

Returns
mixed
Exceptions

InvalidArgumentException if no data were stored by this AsyncTask instance.

Definition at line 228 of file AsyncTask.php.

◆ getResult()

pocketmine\scheduler\AsyncTask::getResult ( )
Returns
mixed

Definition at line 109 of file AsyncTask.php.

◆ hasCancelledRun()

pocketmine\scheduler\AsyncTask::hasCancelledRun ( )
Deprecated:

Definition at line 130 of file AsyncTask.php.

◆ hasResult()

pocketmine\scheduler\AsyncTask::hasResult ( )

Definition at line 102 of file AsyncTask.php.

◆ isCrashed()

pocketmine\scheduler\AsyncTask::isCrashed ( )
Deprecated:

Definition at line 90 of file AsyncTask.php.

◆ isFinished()

pocketmine\scheduler\AsyncTask::isFinished ( )

Returns whether this task has finished executing, whether successfully or not. This differs from isRunning() because it is not true prior to task execution.

Definition at line 98 of file AsyncTask.php.

◆ isSubmitted()

pocketmine\scheduler\AsyncTask::isSubmitted ( )

Definition at line 138 of file AsyncTask.php.

◆ onCompletion()

pocketmine\scheduler\AsyncTask::onCompletion ( )

◆ onError()

pocketmine\scheduler\AsyncTask::onError ( )
Deprecated:
No longer used

Definition at line 196 of file AsyncTask.php.

◆ onProgressUpdate()

pocketmine\scheduler\AsyncTask::onProgressUpdate (   $progress)

Called from the main thread after AsyncTask::publishProgress is called. All AsyncTask::publishProgress calls should result in AsyncTask::onProgressUpdate calls before AsyncTask::onCompletion is called.

Parameters
mixed$progressThe parameter passed to AsyncTask#publishProgress. It is serialize()'ed and then unserialize()'ed, as if it has been cloned.

Definition at line 189 of file AsyncTask.php.

◆ onRun()

◆ publishProgress()

pocketmine\scheduler\AsyncTask::publishProgress ( mixed  $progress)

Call this method from AsyncTask::onRun (AsyncTask execution thread) to schedule a call to AsyncTask::onProgressUpdate from the main thread with the given progress parameter.

Parameters
mixed$progressA value that can be safely serialize()'ed.

Definition at line 161 of file AsyncTask.php.

◆ reallyDestruct()

pocketmine\scheduler\AsyncTask::reallyDestruct ( )
protected

Override this to do normal __destruct() cleanup from a child class.

Definition at line 245 of file AsyncTask.php.

◆ run()

pocketmine\scheduler\AsyncTask::run ( )

Definition at line 78 of file AsyncTask.php.

◆ setResult()

pocketmine\scheduler\AsyncTask::setResult ( mixed  $result)

Definition at line 116 of file AsyncTask.php.

◆ setSubmitted()

pocketmine\scheduler\AsyncTask::setSubmitted ( )

Definition at line 134 of file AsyncTask.php.

◆ storeLocal()

pocketmine\scheduler\AsyncTask::storeLocal ( string  $key,
mixed  $complexData 
)
protected

Saves mixed data in thread-local storage. Data stored using this storage is only accessible from the thread it was stored on. Data stored using this method will not be serialized. This can be used to store references to variables which you need later on on the same thread, but not others.

For example, plugin references could be stored in the constructor of the async task (which is called on the main thread) using this, and then fetched in onCompletion() (which is also called on the main thread), without them becoming serialized.

Scalar types can be stored directly in class properties instead of using this storage.

Objects stored in this storage can be retrieved using fetchLocal() on the same thread that this method was called from.

Definition at line 214 of file AsyncTask.php.

Referenced by pocketmine\network\mcpe\auth\ProcessLoginTask\__construct(), pocketmine\scheduler\BulkCurlTask\__construct(), pocketmine\world\light\LightPopulationTask\__construct(), pocketmine\network\mcpe\ChunkRequestTask\__construct(), pocketmine\world\generator\PopulationTask\__construct(), and pocketmine\network\mcpe\encryption\PrepareEncryptionTask\__construct().

+ Here is the caller graph for this function:

The documentation for this class was generated from the following file: