mirror of
https://github.com/danog/parallel.git
synced 2025-01-22 22:11:11 +01:00
Merge pull request #1 from icicleio/thread-inherit-all
Inherit the environment when spawning a new thread
This commit is contained in:
commit
30882e456b
@ -15,11 +15,6 @@ use Icicle\Loop;
|
||||
*/
|
||||
class Thread extends \Thread
|
||||
{
|
||||
/**
|
||||
* @var string Path to an autoloader to include.
|
||||
*/
|
||||
public $autoloaderPath;
|
||||
|
||||
/**
|
||||
* @var callable The function to execute in the thread.
|
||||
*/
|
||||
@ -43,14 +38,12 @@ class Thread extends \Thread
|
||||
/**
|
||||
* Creates a new thread object.
|
||||
*
|
||||
* @param resource $socket IPC communication socket.
|
||||
* @param resource $socket IPC communication socket.
|
||||
* @param callable $function The function to execute in the thread.
|
||||
* @param mixed[] $args Arguments to pass to the function.
|
||||
* @param string $autoloaderPath Path to autoloader include file.
|
||||
* @param mixed[] $args Arguments to pass to the function.
|
||||
*/
|
||||
public function __construct($socket, callable $function, array $args = [], $autoloaderPath = '')
|
||||
public function __construct($socket, callable $function, array $args = [])
|
||||
{
|
||||
$this->autoloaderPath = $autoloaderPath;
|
||||
$this->function = $function;
|
||||
$this->args = $args;
|
||||
$this->socket = $socket;
|
||||
@ -61,15 +54,24 @@ class Thread extends \Thread
|
||||
*/
|
||||
public function run()
|
||||
{
|
||||
/* First thing we need to do is initialize the class autoloader. If we
|
||||
* don't do this first, objects we receive from other threads will just
|
||||
* be garbage data and unserializable values (like resources) will be
|
||||
* lost. This happens even with thread-safe objects.
|
||||
/* First thing we need to do is re-initialize the class autoloader. If
|
||||
* we don't do this first, any object of a class that was loaded after
|
||||
* the thread started will just be garbage data and unserializable
|
||||
* values (like resources) will be lost. This happens even with
|
||||
* thread-safe objects.
|
||||
*/
|
||||
if ('' !== $this->autoloaderPath) {
|
||||
require $this->autoloaderPath;
|
||||
foreach (get_declared_classes() as $className) {
|
||||
if (strpos($className, 'ComposerAutoloaderInit') === 0) {
|
||||
// Calling getLoader() will register the class loader for us
|
||||
$className::getLoader();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Erase the old event loop inherited from the parent thread and create
|
||||
// a new one.
|
||||
Loop\loop(Loop\create());
|
||||
|
||||
// At this point, the thread environment has been prepared so begin using the thread.
|
||||
$channel = new Channel($this->socket);
|
||||
|
||||
|
@ -40,7 +40,7 @@ class ThreadContext implements ContextInterface
|
||||
list($channel, $socket) = Channel::createSocketPair();
|
||||
|
||||
$this->channel = new Channel($channel);
|
||||
$this->thread = new Thread($socket, $function, $args, $this->getComposerAutoloader());
|
||||
$this->thread = new Thread($socket, $function, $args);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -60,7 +60,7 @@ class ThreadContext implements ContextInterface
|
||||
throw new SynchronizationError('The thread has already been started.');
|
||||
}
|
||||
|
||||
$this->thread->start(PTHREADS_INHERIT_INI);
|
||||
$this->thread->start(PTHREADS_INHERIT_ALL);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -146,28 +146,4 @@ class ThreadContext implements ContextInterface
|
||||
$this->thread->release();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the full path to the Composer autoloader.
|
||||
*
|
||||
* If no Composer autoloader is being used, `null` is returned.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function getComposerAutoloader()
|
||||
{
|
||||
static $path;
|
||||
|
||||
if (null !== $path) {
|
||||
return $path;
|
||||
}
|
||||
|
||||
foreach (get_included_files() as $path) {
|
||||
if (preg_match('/vendor\/autoload.php$/i', $path)) {
|
||||
return $path;
|
||||
}
|
||||
}
|
||||
|
||||
return $path = '';
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user