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

30 lines
781 B
PHP
Raw Normal View History

2016-08-21 23:40:48 -05:00
<?php declare(strict_types = 1);
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
2016-08-23 16:47:40 -05:00
use Amp\Parallel\{ Forking\Fork, Threading\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 WorkerFork will be created if forking is available, otherwise
* a WorkerProcess will be created.
*/
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
}
2016-08-18 11:04:48 -05:00
if (Fork::supported()) {
2016-08-22 18:25:19 -05:00
return new WorkerFork;
2015-12-04 23:50:32 -06:00
}
2016-08-22 18:25:19 -05:00
return new WorkerProcess;
2015-12-04 23:50:32 -06:00
}
}