📜 ⬆️ ⬇️

Simultaneously || At the same time

image

Introduction


ZendX_Console_Process_Unix allows developers to create objects under the guise of processes, and thus perform several tasks in parallel. Due to the current implementation features, this feature is available only on * nix systems, such as Linux, Solaris, Mac / OSX, and others in the CLI or CGI mode. In addition, the following modules are required for this component to work: Shared Memory , Process Control, and POSIX . If one of the requirements is not met, an exception will be thrown.

Basics of using ZendX_Console_Process_Unix


ZendX_Console_Process_Unix is ​​an abstract class that must be extended by the user. It contains the only abstract method _run (), which should describe the implemented algorithm for processing certain data. Also in the class are defined methods for checking the state of the process and exchanging variables between the main and child processes.

The _run () method, as well as each method called inside it will be executed in a separate process. Any other method invoked in your application will be executed in the parent process.
')
The setVariable () and getVariable () methods can be used to transfer variables between parent and child processes. To monitor the status of the child process, you should call the _setAlive () method inside it at short intervals, so the parent process can get information about the state of the child process by calling the getLastAlive () method. To get the PID of the child process, the parent can call getPid ().

class MyProcess extends ZendX_Console_Process_Unix { protected function _run() { for ($i = 0; $i < 10; $i++) { // Doing something really important which can't wait: sleeping sleep(1); } } } // This part should last about 10 seconds, not 20. $process1 = new MyProcess(); $process1->start(); $process2 = new MyProcess(); $process2->start(); while ($process1->isRunning() && $process2->isRunning()) { sleep(1); } echo 'All processes completed' ; * This source code was highlighted with Source Code Highlighter .
  1. class MyProcess extends ZendX_Console_Process_Unix { protected function _run() { for ($i = 0; $i < 10; $i++) { // Doing something really important which can't wait: sleeping sleep(1); } } } // This part should last about 10 seconds, not 20. $process1 = new MyProcess(); $process1->start(); $process2 = new MyProcess(); $process2->start(); while ($process1->isRunning() && $process2->isRunning()) { sleep(1); } echo 'All processes completed' ; * This source code was highlighted with Source Code Highlighter .
  2. class MyProcess extends ZendX_Console_Process_Unix { protected function _run() { for ($i = 0; $i < 10; $i++) { // Doing something really important which can't wait: sleeping sleep(1); } } } // This part should last about 10 seconds, not 20. $process1 = new MyProcess(); $process1->start(); $process2 = new MyProcess(); $process2->start(); while ($process1->isRunning() && $process2->isRunning()) { sleep(1); } echo 'All processes completed' ; * This source code was highlighted with Source Code Highlighter .
  3. class MyProcess extends ZendX_Console_Process_Unix { protected function _run() { for ($i = 0; $i < 10; $i++) { // Doing something really important which can't wait: sleeping sleep(1); } } } // This part should last about 10 seconds, not 20. $process1 = new MyProcess(); $process1->start(); $process2 = new MyProcess(); $process2->start(); while ($process1->isRunning() && $process2->isRunning()) { sleep(1); } echo 'All processes completed' ; * This source code was highlighted with Source Code Highlighter .
  4. class MyProcess extends ZendX_Console_Process_Unix { protected function _run() { for ($i = 0; $i < 10; $i++) { // Doing something really important which can't wait: sleeping sleep(1); } } } // This part should last about 10 seconds, not 20. $process1 = new MyProcess(); $process1->start(); $process2 = new MyProcess(); $process2->start(); while ($process1->isRunning() && $process2->isRunning()) { sleep(1); } echo 'All processes completed' ; * This source code was highlighted with Source Code Highlighter .
  5. class MyProcess extends ZendX_Console_Process_Unix { protected function _run() { for ($i = 0; $i < 10; $i++) { // Doing something really important which can't wait: sleeping sleep(1); } } } // This part should last about 10 seconds, not 20. $process1 = new MyProcess(); $process1->start(); $process2 = new MyProcess(); $process2->start(); while ($process1->isRunning() && $process2->isRunning()) { sleep(1); } echo 'All processes completed' ; * This source code was highlighted with Source Code Highlighter .
  6. class MyProcess extends ZendX_Console_Process_Unix { protected function _run() { for ($i = 0; $i < 10; $i++) { // Doing something really important which can't wait: sleeping sleep(1); } } } // This part should last about 10 seconds, not 20. $process1 = new MyProcess(); $process1->start(); $process2 = new MyProcess(); $process2->start(); while ($process1->isRunning() && $process2->isRunning()) { sleep(1); } echo 'All processes completed' ; * This source code was highlighted with Source Code Highlighter .
  7. class MyProcess extends ZendX_Console_Process_Unix { protected function _run() { for ($i = 0; $i < 10; $i++) { // Doing something really important which can't wait: sleeping sleep(1); } } } // This part should last about 10 seconds, not 20. $process1 = new MyProcess(); $process1->start(); $process2 = new MyProcess(); $process2->start(); while ($process1->isRunning() && $process2->isRunning()) { sleep(1); } echo 'All processes completed' ; * This source code was highlighted with Source Code Highlighter .
  8. class MyProcess extends ZendX_Console_Process_Unix { protected function _run() { for ($i = 0; $i < 10; $i++) { // Doing something really important which can't wait: sleeping sleep(1); } } } // This part should last about 10 seconds, not 20. $process1 = new MyProcess(); $process1->start(); $process2 = new MyProcess(); $process2->start(); while ($process1->isRunning() && $process2->isRunning()) { sleep(1); } echo 'All processes completed' ; * This source code was highlighted with Source Code Highlighter .
  9. class MyProcess extends ZendX_Console_Process_Unix { protected function _run() { for ($i = 0; $i < 10; $i++) { // Doing something really important which can't wait: sleeping sleep(1); } } } // This part should last about 10 seconds, not 20. $process1 = new MyProcess(); $process1->start(); $process2 = new MyProcess(); $process2->start(); while ($process1->isRunning() && $process2->isRunning()) { sleep(1); } echo 'All processes completed' ; * This source code was highlighted with Source Code Highlighter .
  10. class MyProcess extends ZendX_Console_Process_Unix { protected function _run() { for ($i = 0; $i < 10; $i++) { // Doing something really important which can't wait: sleeping sleep(1); } } } // This part should last about 10 seconds, not 20. $process1 = new MyProcess(); $process1->start(); $process2 = new MyProcess(); $process2->start(); while ($process1->isRunning() && $process2->isRunning()) { sleep(1); } echo 'All processes completed' ; * This source code was highlighted with Source Code Highlighter .
  11. class MyProcess extends ZendX_Console_Process_Unix { protected function _run() { for ($i = 0; $i < 10; $i++) { // Doing something really important which can't wait: sleeping sleep(1); } } } // This part should last about 10 seconds, not 20. $process1 = new MyProcess(); $process1->start(); $process2 = new MyProcess(); $process2->start(); while ($process1->isRunning() && $process2->isRunning()) { sleep(1); } echo 'All processes completed' ; * This source code was highlighted with Source Code Highlighter .
  12. class MyProcess extends ZendX_Console_Process_Unix { protected function _run() { for ($i = 0; $i < 10; $i++) { // Doing something really important which can't wait: sleeping sleep(1); } } } // This part should last about 10 seconds, not 20. $process1 = new MyProcess(); $process1->start(); $process2 = new MyProcess(); $process2->start(); while ($process1->isRunning() && $process2->isRunning()) { sleep(1); } echo 'All processes completed' ; * This source code was highlighted with Source Code Highlighter .
  13. class MyProcess extends ZendX_Console_Process_Unix { protected function _run() { for ($i = 0; $i < 10; $i++) { // Doing something really important which can't wait: sleeping sleep(1); } } } // This part should last about 10 seconds, not 20. $process1 = new MyProcess(); $process1->start(); $process2 = new MyProcess(); $process2->start(); while ($process1->isRunning() && $process2->isRunning()) { sleep(1); } echo 'All processes completed' ; * This source code was highlighted with Source Code Highlighter .
  14. class MyProcess extends ZendX_Console_Process_Unix { protected function _run() { for ($i = 0; $i < 10; $i++) { // Doing something really important which can't wait: sleeping sleep(1); } } } // This part should last about 10 seconds, not 20. $process1 = new MyProcess(); $process1->start(); $process2 = new MyProcess(); $process2->start(); while ($process1->isRunning() && $process2->isRunning()) { sleep(1); } echo 'All processes completed' ; * This source code was highlighted with Source Code Highlighter .
  15. class MyProcess extends ZendX_Console_Process_Unix { protected function _run() { for ($i = 0; $i < 10; $i++) { // Doing something really important which can't wait: sleeping sleep(1); } } } // This part should last about 10 seconds, not 20. $process1 = new MyProcess(); $process1->start(); $process2 = new MyProcess(); $process2->start(); while ($process1->isRunning() && $process2->isRunning()) { sleep(1); } echo 'All processes completed' ; * This source code was highlighted with Source Code Highlighter .
  16. class MyProcess extends ZendX_Console_Process_Unix { protected function _run() { for ($i = 0; $i < 10; $i++) { // Doing something really important which can't wait: sleeping sleep(1); } } } // This part should last about 10 seconds, not 20. $process1 = new MyProcess(); $process1->start(); $process2 = new MyProcess(); $process2->start(); while ($process1->isRunning() && $process2->isRunning()) { sleep(1); } echo 'All processes completed' ; * This source code was highlighted with Source Code Highlighter .
  17. class MyProcess extends ZendX_Console_Process_Unix { protected function _run() { for ($i = 0; $i < 10; $i++) { // Doing something really important which can't wait: sleeping sleep(1); } } } // This part should last about 10 seconds, not 20. $process1 = new MyProcess(); $process1->start(); $process2 = new MyProcess(); $process2->start(); while ($process1->isRunning() && $process2->isRunning()) { sleep(1); } echo 'All processes completed' ; * This source code was highlighted with Source Code Highlighter .
  18. class MyProcess extends ZendX_Console_Process_Unix { protected function _run() { for ($i = 0; $i < 10; $i++) { // Doing something really important which can't wait: sleeping sleep(1); } } } // This part should last about 10 seconds, not 20. $process1 = new MyProcess(); $process1->start(); $process2 = new MyProcess(); $process2->start(); while ($process1->isRunning() && $process2->isRunning()) { sleep(1); } echo 'All processes completed' ; * This source code was highlighted with Source Code Highlighter .
  19. class MyProcess extends ZendX_Console_Process_Unix { protected function _run() { for ($i = 0; $i < 10; $i++) { // Doing something really important which can't wait: sleeping sleep(1); } } } // This part should last about 10 seconds, not 20. $process1 = new MyProcess(); $process1->start(); $process2 = new MyProcess(); $process2->start(); while ($process1->isRunning() && $process2->isRunning()) { sleep(1); } echo 'All processes completed' ; * This source code was highlighted with Source Code Highlighter .
class MyProcess extends ZendX_Console_Process_Unix { protected function _run() { for ($i = 0; $i < 10; $i++) { // Doing something really important which can't wait: sleeping sleep(1); } } } // This part should last about 10 seconds, not 20. $process1 = new MyProcess(); $process1->start(); $process2 = new MyProcess(); $process2->start(); while ($process1->isRunning() && $process2->isRunning()) { sleep(1); } echo 'All processes completed' ; * This source code was highlighted with Source Code Highlighter .

In this example, the process is parallelized in two and started. Each child process runs for 10 seconds, so the parent process will be completed within 10 seconds (and not 20).

PS
Despite the fact that I actively use the Zend Framework and the fact that this component has been available for more than 16 months (since October 31, 2008), I learned about its existence only now, maybe now the curl bots will become even meaner.

Source: https://habr.com/ru/post/87093/


All Articles