AsyncWorker
extends Worker
in package
Specialized Worker class for PocketMine-MP-related use cases. It handles setting up autoloading and error handling.
Workers are a special type of thread which execute tasks passed to them during their lifetime. Since creating a new thread has a high resource cost, workers can be kept around and reused for lots of short-lived tasks.
As a plugin developer, you'll rarely (if ever) actually need to use this class directly. If you want to run tasks on other CPU cores, check out AsyncTask first.
Table of Contents
- $composerAutoloaderPath : string|null
- $isKilled : bool
- $classLoaders : Threaded|null
- $id : int
- $logger : ThreadedLogger
- $memoryLimit : int
- $notifier : SleeperNotifier
- $store : array<string|int, mixed>
- __construct() : mixed
- getAsyncWorkerId() : int
- getClassLoaders() : array<string|int, ClassLoader>
- getFromThreadStore() : mixed
- Retrieves mixed data from the worker's thread-local object store.
- getLogger() : ThreadedLogger
- getNotifier() : SleeperNotifier
- getThreadName() : string
- handleException() : void
- quit() : void
- Stops the thread using the best way possible. Try to stop it yourself before calling this.
- registerClassLoaders() : void
- Registers the class loaders for this thread.
- removeFromThreadStore() : void
- Removes previously-stored mixed data from the worker's thread-local object store.
- run() : void
- saveToThreadStore() : void
- Saves mixed data into the worker's thread-local object store. This can be used to store objects which you want to use on this worker thread from multiple AsyncTasks.
- setClassLoaders() : void
- start() : bool
- onRun() : void
Properties
$composerAutoloaderPath
protected
string|null
$composerAutoloaderPath
$isKilled
protected
bool
$isKilled
= false
$classLoaders
private
Threaded|null
$classLoaders
= null
$id
private
int
$id
$logger
private
ThreadedLogger
$logger
$memoryLimit
private
int
$memoryLimit
$notifier
private
SleeperNotifier
$notifier
$store
private
static array<string|int, mixed>
$store
= []
Methods
__construct()
public
__construct(ThreadedLogger $logger, int $id, int $memoryLimit, SleeperNotifier $notifier) : mixed
Parameters
- $logger : ThreadedLogger
- $id : int
- $memoryLimit : int
- $notifier : SleeperNotifier
Return values
mixed —getAsyncWorkerId()
public
getAsyncWorkerId() : int
Return values
int —getClassLoaders()
public
getClassLoaders() : array<string|int, ClassLoader>
Return values
array<string|int, ClassLoader> —getFromThreadStore()
Retrieves mixed data from the worker's thread-local object store.
public
getFromThreadStore(string $identifier) : mixed
Note that the thread-local object store could be cleared and your data might not exist, so your code should account for the possibility that what you're trying to retrieve might not exist.
Objects stored in this storage may ONLY be retrieved while the task is running.
Parameters
- $identifier : string
Return values
mixed —getLogger()
public
getLogger() : ThreadedLogger
Return values
ThreadedLogger —getNotifier()
public
getNotifier() : SleeperNotifier
Return values
SleeperNotifier —getThreadName()
public
getThreadName() : string
Return values
string —handleException()
public
handleException(Throwable $e) : void
Parameters
- $e : Throwable
Return values
void —quit()
Stops the thread using the best way possible. Try to stop it yourself before calling this.
public
quit() : void
Return values
void —registerClassLoaders()
Registers the class loaders for this thread.
public
registerClassLoaders() : void
WARNING: This method MUST be called from any descendent threads' run() method to make autoloading usable. If you do not do this, you will not be able to use new classes that were not loaded when the thread was started (unless you are using a custom autoloader).
Return values
void —removeFromThreadStore()
Removes previously-stored mixed data from the worker's thread-local object store.
public
removeFromThreadStore(string $identifier) : void
Parameters
- $identifier : string
Return values
void —run()
public
final run() : void
Return values
void —saveToThreadStore()
Saves mixed data into the worker's thread-local object store. This can be used to store objects which you want to use on this worker thread from multiple AsyncTasks.
public
saveToThreadStore(string $identifier, mixed $value) : void
Parameters
- $identifier : string
- $value : mixed
Return values
void —setClassLoaders()
public
setClassLoaders([array<string|int, ClassLoader> $autoloaders = null ]) : void
Parameters
- $autoloaders : array<string|int, ClassLoader> = null
Return values
void —start()
public
start([int $options = PTHREADS_INHERIT_NONE ]) : bool
Parameters
- $options : int = PTHREADS_INHERIT_NONE
Return values
bool —onRun()
protected
onRun() : void