1
0
mirror of https://github.com/danog/parallel.git synced 2024-11-27 04:44:56 +01:00
parallel/lib/Worker/DefaultWorkerFactory.php

30 lines
781 B
PHP
Raw Normal View History

2016-08-22 06:40:48 +02:00
<?php declare(strict_types = 1);
2015-12-05 06:50:32 +01:00
2016-08-23 23:47:40 +02:00
namespace Amp\Parallel\Worker;
2016-08-18 18:04:48 +02:00
2016-08-23 23:47:40 +02:00
use Amp\Parallel\{ Forking\Fork, Threading\Thread };
2015-12-05 08:09:42 +01:00
2015-12-05 06:50:32 +01:00
/**
* The built-in worker factory type.
*/
2016-08-18 18:04:48 +02:00
class DefaultWorkerFactory implements WorkerFactory {
2015-12-05 06:50:32 +01: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 WorkerFork will be created if forking is available, otherwise
* a WorkerProcess will be created.
*/
2016-08-18 18:04:48 +02:00
public function create(): Worker {
if (Thread::supported()) {
2016-08-23 01:25:19 +02:00
return new WorkerThread;
2015-12-05 06:50:32 +01:00
}
2016-08-18 18:04:48 +02:00
if (Fork::supported()) {
2016-08-23 01:25:19 +02:00
return new WorkerFork;
2015-12-05 06:50:32 +01:00
}
2016-08-23 01:25:19 +02:00
return new WorkerProcess;
2015-12-05 06:50:32 +01:00
}
}