spatie / async

Easily run code asynchronously
https://spatie.be/en/opensource/php
MIT License
2.64k stars 178 forks source link

How to get output from the callback, so I can set it into variable #147

Closed ardzz closed 2 years ago

ardzz commented 3 years ago
$pool = Pool::create()->concurrency(20);
        foreach ($paginations as $pagination){
            $pool->add(function () use ($pagination){
                 return Handler::Response(function () use ($pagination){
                    return Request::create()->get("ongoing-anime/page/{$pagination}/");
                 });
             });
        }
        $pool->wait();

So I want to get request into pagination pages then I will set the output of request into some variable, how I do to get the output?

Ahzam-Lathiya commented 3 years ago

@ardzz I'm stuck with something familiar. Why does the pool->add() function expect a complete function body, instead What if we want to pass in the callable string\array?

Something Like this: `function do_something() { $x = $x + 1; return $x; }

$pool->add('do_something')->then() .....`

Whenever i try to do this I get error PHP Fatal error: Uncaught TypeError: call_user_func(): Argument #1 ($function) must be a valid callback, function "do_something" not found or invalid function name

and when I call the function inside. pool->add(do_something())

I get this error: PHP Fatal error: Uncaught InvalidArgumentException: The process passed to Pool::add should be callable. in /home/ahzam/phppractice/async/vendor/spatie/async/src/Pool.php:136

Please suggest a workaround for this because I don't want to write the entire function body inside pool->add(), instead I want to define my functions elsewhere and just call their names inside.

Thanks

ardzz commented 3 years ago

try this

function do_something()
{
     $x = $x + 1;
     return $x;
}
$pool->add(Closure::fromCallable('do_something'))->then()...
Ahzam-Lathiya commented 3 years ago

@ardzz I followed your solution above. Still I get error: PHP Fatal error: Uncaught Error: Call to a member function bindTo() on null

Instead I used Anonymous function like:

$x = function()
{
  return 'name';
};

$pool->add($x)->then(function ($output) {
  echo $output;
});

Thanks

By the way do you know how to add class methods and static methods instead in the pool like above.

Stevemoretz commented 2 years ago

@ardzz I followed your solution above. Still I get error: PHP Fatal error: Uncaught Error: Call to a member function bindTo() on null

Instead I used Anonymous function like:

$x = function()
{
  return 'name';
};

$pool->add($x)->then(function ($output) {
  echo $output;
});

Thanks

By the way do you know how to add class methods and static methods instead in the pool like above.

Just call your static methods and normal methods and whatever you want inside the closure, but you need to autoload all those things that you use inside your closure.

If you can't use the autoloading method try building a closure from your methods : https://stackoverflow.com/a/44751391/10268067 However I'm not sure if you do this second method the serialization would go right, you might get an error, but the first method always works.

You must know that underneath this is used : https://github.com/opis/closure It's not magic it's just serialization.

TheTechsTech commented 2 years ago

Might just be simpler to update your composer.json to point to some file with some functions you always need, and insure all new classes/namespaces are within also.

This makes calling ParentRuntime::init() or Pool::autoload() directly by user unnecessary.

// composer.json

"autoload": {
    "files": [
        "Folder/functions.php"
    ],
    "psr-4": {
        "Name\\Space\\": "Folder/"
    }
}

// functions.php

if (!\function_exists('__placemarker')) {
  //
  // functions needed in child process...
  //

  function __placemarker()
  {
    return true;
  }
}

This will resolve issues https://github.com/spatie/async/issues/162, https://github.com/spatie/async/issues/164, and https://github.com/spatie/async/issues/168 also.

spatie-bot commented 2 years ago

Dear contributor,

because this issue seems to be inactive for quite some time now, I've automatically closed it. If you feel this issue deserves some attention from my human colleagues feel free to reopen it.