amphp / parallel

An advanced parallelization library for PHP, enabling efficient multitasking, optimizing resource use, and application responsiveness through multiple CPU threads.
MIT License
779 stars 64 forks source link

How to include my "getDetails" function which takes a value (here a variable: "$href") as a parameter in Worker\enqueueCallable ??? #147

Closed chegmarco1989 closed 1 year ago

chegmarco1989 commented 2 years ago

Hello.

We are trying to use this amPHP-Parallel library (https://github.com/amphp/parallel) to our getDetails($href) function. Knowing that the README used the file_get_contents function in the promises as follows:

$urls = [
    'https://secure.php.net',
    'https://amphp.org',
    'https://github.com',
];

$promises = [];
foreach ($urls as $url) {
    $promises[$url] = Worker\enqueueCallable('file_get_contents', $url);
}

$responses = Promise\wait(Promise\all($promises));

foreach ($responses as $url => $response) {
    \printf("Read %d bytes from %s\n", \strlen($response), $url);
}

While my getDetails($href) function, which I am trying to process in parallel, as you can see, takes as a parameter or argument between brackets, a value (here represented by the variable $href), how then, can I include it in the Worker\enqueueCallable() to replace the default file_get_contents ???

Please enlighten me please.

trowski commented 1 year ago

Apologies that this issue did not receive more immediate attention.

To use a function in a worker pool, you must define the function in a way that the Composer autoloader can load. In general, I'd recommend exploring the Task interface instead to define custom logic to execute in a worker.

Seeing as this issue is old, I'm going to close it. If you need further support, please open another issue (and I'll try to reply a bit faster) 👍

chegmarco1989 commented 1 year ago

Could you give me a usage example for my getDetails($href) function ???

Please guide me. Awaiting your reply.