Patterns for PHP Multi Processes

Patterns for PHP multi processes?

If your goal is minimal time - the solution is simple to describe, but not that simple to implement.

You need to find a pattern to divide the work (You don't provide much information in the question in this regard).

Then use one master process that forks children to do the work. As a rule the total number of processes you use should be between n and 2n, where n is the number of cores the machine has.

Assuming this data will be stored in files you might consider using non-blocking IO to maximize the throughput. Not doing so will make most of your process spend time waiting for the disk. PHP has stream_select() that might help you. Note that using it is not trivial.

If you decide not to use select - increasing the number of processes might help.


In regards to pcntl functions: I've written a deamon with them (a proper one with forking, changing session id, the running user, etc...) and it's one of the most reliable piece of software I've written. Because it spawns workers for every task, even if there is a bug in one of the tasks, it does not affect the others.

Design Pattern: Which pattern is suitable for FileProcessor design that reads XLSX file and can also read a zip file that contains multiple XLSX files

IMHO the strategy pattern is more suited for this use case tomorrow you will have another file type to process and so on https://refactoring.guru/design-patterns/strategy

PHP Design Pattern Advice - many dependant steps that rely on results from previous steps

For anyone who is interested, I did finally stumble across a design pattern that suited my needs.

My question could have been condensed down to simply:

How do you collect the results of a complicated operation that is spread over several objects?

(I have now edited the question)

The answer was the Collecting Parameter Design Pattern.
http://sourcemaking.com/implementation-patterns/collecting-parameter

There are not too many examples in the PHP world although its not a very complicated concept. Common sense really but I guess thats what all Design Patterns are.

The way I solved it was by implementing each separate operation using the Command Pattern which abstracts a single operation so that I can invoke it using a common interface (i.e. a "process()" or "run()" method).

Given I now have 12 operations that I can invoke with their own process() method, I trigger them in order and I pass in a generic object (called CollectingParameter for clarity) whose job is simply to record the result of each operation (so the CollectingParameter object must be returned by each process() method). The CollectingParameter object gets passed into the next operation which will then have access to all of the results it needs from the previous operations.

$operationList = array(new CreateCustomer, new CreateOrder, new CreateOrderLines ... etc)
$collectingParameter = new CollectingParameter;
foreach($operationList as $operation) {
$collectingParameter = $operation->process($collectingParameter);
}

Errors are logged and handled within the various process methods which leaves the process() method free to return the CollectingParameter object rather than the results of the operation.

How to define multiple patterns in php glob()

You can use the GLOB_BRACE constant

GLOB_BRACE - Expands {a,b,c} to match 'a', 'b', or 'c'

e.g.

$dirname = 'uploads/';
glob("$dirname*.{png,jpeg,jpg,gif}", GLOB_BRACE);

See: http://php.net/manual/en/function.glob.php

preg_split and multiple patterns

Using preg_split() you can achieve this using a reluctant quantifier with your {n,m} repetition, the \K escape sequence and a Positive Lookahead.

$results = preg_split('/^[a-z0-9]{2,6}?\K(?=[0-9]{1,3}$)/i', $string);
print_r($results);

Live Demo ( or see a working demo )

multiple patterns with preg_match

Your code worked with me, and I didn't find any problem with the code or the REGEX. Furthermore, the description you provided is not enough to understand your needs.

However, I have guessed one problem after observing your code, which is, you didn't use any anchor(^...$) to perform matching the whole string. Your regex can find match for these inputs: %ABC01DC4V or ABCDEF4EE. So change this line with your code:

$regex = '/^(' .implode('|', $patterns) .')$/i'; 
-+- -+-

How do I search for patterns in multiple arrays in PHP?

I would use preg_grep:

http://php.net/manual/en/function.preg-grep.php

Regexp pattern to match (and remove) multi-line PHP code from include files

Try the following regular expression (replace with ""):

/\n?<\?(php|=)?(.*?)\?>\n?/ms

Explained:

\n?       - Tests for a newline
< - Tests for start tag
\? - Tests for '?' after the start tag
(php|=)? - Tests for the 'php' or '=' after the start tag
(.*?) - Tests for any PHP code
\? - Tests for end tag
\n? - Tests for a newline
/ms - Allows multiple lines

EDIT: Fixed Multiline Support



Related Topics



Leave a reply



Submit