/** * Spawn new worker processes * @param $n - integer - number of workers to spawn * @return boolean - success */ public function spawnWorkers($n) { if (FS::$supported) { eio_event_loop(); } $n = (int) $n; for ($i = 0; $i < $n; ++$i) { $thread = new Daemon_WorkerThread(); $this->workers->push($thread); $this->callbacks->push(function ($self) use($thread) { $pid = $thread->start(); if ($pid < 0) { Daemon::log('could not fork worker'); } elseif ($pid === 0) { // worker Daemon::log('Unexcepted execution return to outside of Thread_start()'); exit; } }); } return true; }
/** * Spawn IPC process * @param $n - integer - number of workers to spawn * @return boolean - success */ protected function spawnIPCThread() { if (FileSystem::$supported) { eio_event_loop(); } $thread = new IPC(); $this->ipcthreads->push($thread); $this->callbacks->push(function ($self) use($thread) { $thread->start(); $pid = $thread->getPid(); if ($pid < 0) { Daemon::$process->log('could not fork IPCThread'); } elseif ($pid === 0) { // worker $this->log('Unexcepted execution return to outside of Thread->start()'); exit; } }); if ($this->eventBase) { $this->eventBase->stop(); } return true; }
public function test_readfile_eio() { $time_start = microtime(true); echo "reading file is started....<br/>"; $file_open_cb = function ($data, $result) { eio_read($result, 10485760, 0, EIO_PRI_DEFAULT, function ($data, $result) { echo str_word_count($result) . "<br/>"; eio_close($data); }, $result); }; $filename = './assets/data/access.log'; eio_open($filename, EIO_O_RDWR, NULL, EIO_PRI_DEFAULT, $file_open_cb, $filename); $filename = './assets/data/error.log'; eio_open($filename, EIO_O_RDWR, NULL, EIO_PRI_DEFAULT, $file_open_cb, $filename); echo "reading file is finish....<br/>"; eio_event_loop(); $time_end = microtime(true); $time = $time_end - $time_start; echo "Execution time: {$time} seconds<br/>"; }
/** * Doesn't do anything unless the eio extension is loaded * * Will block until the sync is complete * * @throws IOException If the sync fails * @return void */ public function sync() { if (!$this->file) { throw new IOException('Cannot sync file: file is not open'); } if (extension_loaded('eio')) { $success = false; eio_fsync($this->file, null, function ($data, $result) use(&$success) { if ($result === 0) { $success = true; } }); eio_event_loop(); if (!$success) { throw new IOException('Failed to sync file to disk'); } } }