# Running several processes in PHP

# Problem

I had to analyze and read 14739 folders using PHP, which took over half an hour.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1735321770204/4ffe609e-d69a-4969-ab94-5d27252c37ce.png align="center")

While PHP has some async functionalities, they don’t work in Windows. However, some solutions are still available, such as using CURL calls, exec calls, and COM calls, but, these are based on calling the functionality without getting an answer. What progress does it have? What if the operation failed?

So, one solution is to use JavaScript and PHP in tandem. Why?

* JavaScript could coordinate a visual interface and the async calls.
    
* While PHP does the job.
    

Then I created this simple library:

[EFTEC/MultiOne: MultiOne is a library to execute concurrent tasks programmed in PHP using JavaScript](https://github.com/EFTEC/MultiOne)

## How does it work?

It works in 3 stages:

* Starting the jobs and creating the payload for every worker. A **worker** is the process that does its job.
    
* Every worker does his job
    
* And when every worker ends, you can do a final operation.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1735821037411/456d28c1-1000-4cdd-a425-1b8d455d7a45.png align="center")

The code:

First, we create an instance of the class

```php
use Eftec\MultiOne\MultiOne;
$work=MultiOne::Factory(
    1000, // miliseconds
    basename(__FILE__), // the file to call (itself)
    4 // number of workers
);
```

And we want to call the next 3 functions

Create the initial works (4 workers, 4 jobs). It could be any value and not just arrays.

In this case, every worker will be assigned an array or values.

```php
    static function($numWorkers) {
        $work=[
            ["apple","banana","pear","orange","lemon"],
            ["alpha","beta","gamma","delta"],
            ["tango","charlie","foxtrot"],
            [1,2,3,4,5,6,7,8,9,10,11,12],
        ];
        return $work; // we give every worker a work
    }
```

Second, we do the work. In this case, we pop an element at the end of the array and we display it. If the array is empty, then we send an “end” command (we stop the worker)

```php
static function($idWorker, $payload) {
        usleep(500);
        $current=array_pop($payload); // apple, banana, etc.
        if($current===null) {
            return MultiOne::msgAnswer('end',$payload,$current.' work done');
        }
        return MultiOne::msgAnswer('run',$payload,$current);
    }
```

And third, when every worker has stopped, we could do a final operation. In this case, we are only returning a simple message.

```php
static function($payloads) {
    return 'work done';
}
```

We add those 3 functions as follows

```php
$work->setMethods(
    static function($numWorkers) {
        $work=[
            ["apple","banana","pear","orange","lemon"],
            ["alpha","beta","gamma","delta"],
            ["tango","charlie","foxtrot"],
            [1,2,3,4,5,6,7,8,9,10,11,12],
        ];
        return $work; // we give every worker a work
    },
    static function($idWorker, $payload) {
        usleep(500);
        $current=array_pop($payload);
        if($current===null) {
            return MultiOne::msgAnswer('end',$payload,$current.' work done');
        }
        return MultiOne::msgAnswer('run',$payload,$current);
    },
    static function($payloads) {
        return 'work done';
    }
);
```

Finally, we execute the class

```php
$work->runAuto();
```

Our entire code will look as follows

```php
$work=MultiOne::Factory(
    1000, // miliseconds
    basename(__FILE__), // the file to call (itself)
    4 // number of workers
);
$work->setMethods(
    static function($numWorkers) {
        $work=[
            ["apple","banana","pear","orange","lemon"],
            ["alpha","beta","gamma","delta"],
            ["tango","charlie","foxtrot"],
            [1,2,3,4,5,6,7,8,9,10,11,12],
        ];
        return $work; // we give every worker a work
    },
    static function($idWorker, $payload) {
        usleep(500);
        $current=array_pop($payload);
        if($current===null) {
            return MultiOne::msgAnswer('end',$payload,$current.' work done');
        }
        return MultiOne::msgAnswer('run',$payload,$current);
    },
    static function($payloads) {
        return 'work done';
    }
);
$work->runAuto();
```

If we call this PHP file, it will show the next values

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1735822473979/ac5ad3ed-7a74-4b9a-8290-ad6d24b5495a.png align="center")

**It’s simple and it works**.

A new version

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1735823819729/e249f6d9-b67d-482c-ab3a-d8b6191839c1.png align="center")

Code:

```php
$work=MultiOne::Factory(
    1000, // miliseconds
    basename(__FILE__), // the file to call (itself)
    4 // number of workers
);
$work->setMethods(
    static function($numWorkers) {
        $work=[
            [["apple","banana","pear","orange","lemon"],5],
            [["alpha","beta","gamma","delta"],4],
            [["tango","charlie","foxtrot"],3],
            [[1,2,3,4,5,6,7,8,9,10,11,12],12],
        ];
        return $work; // we give every worker a work
    },
    static function($idWorker, $payload) {
        usleep(500);
        $txt=str_repeat('📦',count($payload[0])).str_repeat('✅',$payload[1]-count($payload[0]));
        $current=array_pop($payload[0]);
        if($current===null) {
            return MultiOne::msgAnswer('end',$payload,$txt);
        }
        return MultiOne::msgAnswer('run',$payload,$txt);
    },
    static function($payloads) {
        return 'work done';
    }
);
$work->runAuto();
```
