1
0
mirror of https://github.com/danog/parallel.git synced 2025-01-23 06:21:12 +01:00
parallel/lib/Worker/DefaultWorkerFactory.php

31 lines
934 B
PHP
Raw Normal View History

2016-12-29 19:16:04 -06:00
<?php
2015-12-04 23:50:32 -06:00
2016-08-23 16:47:40 -05:00
namespace Amp\Parallel\Worker;
2016-08-18 11:04:48 -05:00
2017-11-29 17:58:00 -06:00
use Amp\Parallel\Context\Thread;
2015-12-05 01:09:42 -06:00
2015-12-04 23:50:32 -06:00
/**
* The built-in worker factory type.
*/
2016-08-18 11:04:48 -05:00
class DefaultWorkerFactory implements WorkerFactory {
2015-12-04 23:50:32 -06:00
/**
* {@inheritdoc}
*
* The type of worker created depends on the extensions available. If multi-threading is enabled, a WorkerThread
* will be created. If threads are not available a WorkerProcess will be created.
*
* \Amp\Parallel\Forking\WorkerFork is not used in the default factory since forking a process for workers is very
* sensitive to timing and process state and should be used only by the application designer if desired.
2015-12-04 23:50:32 -06:00
*/
2016-08-18 11:04:48 -05:00
public function create(): Worker {
if (Thread::supported()) {
2016-08-22 18:25:19 -05:00
return new WorkerThread;
2015-12-04 23:50:32 -06:00
}
return new WorkerProcess(
\getenv("AMP_PHP_BINARY") ?:
\defined("AMP_PHP_BINARY") ? \AMP_PHP_BINARY : null
);
2015-12-04 23:50:32 -06:00
}
}