parallel icon indicating copy to clipboard operation
parallel copied to clipboard

Is parallel work with swoole ?

Open webrobot1 opened this issue 1 year ago • 4 comments

Try ti use pnly in main process - all ok. Try to use in secondary thread only - get error "async-io must be used in PHP CLI mode"

I check sapi_name() before error - and it still "cli"

<?php
use parallel\{Channel, Runtime, Future, Events, Events\Event, Events\Input};

$future = (new Runtime())->run
(
	function()
	{
		echo php_sapi_name().PHP_EOL;
		
		$function = function(){sleep(1);};
		\Swoole\Coroutine::create($function);
		
		
		echo "Finish";
	}
);
sleep(3);

echo $future->value();
$future->cancel();

webrobot1 avatar Jul 07 '23 15:07 webrobot1

Error generate this swoole method https://github.com/swoole/swoole-src/blob/9a0c19815358801febd495ed4c8b423df930ea74/ext-src/swoole_event.cc#L219

webrobot1 avatar Jul 07 '23 15:07 webrobot1

And if i understand right varible what we in CLI mode assign in swoole here https://github.com/swoole/swoole-src/blob/9a0c19815358801febd495ed4c8b423df930ea74/ext-src/php_swoole.cc#L698C17-L698C20

  • In Zend runtime function "PHP_MINIT_FUNCTION"

webrobot1 avatar Jul 07 '23 15:07 webrobot1

but if create any coroutime before create Runtime it is work inside the thread too....

<?php
use parallel\{Channel, Runtime, Future, Events, Events\Event, Events\Input};

$function = function(){sleep(1);};
\Swoole\Coroutine::create($function);
		
$future = (new Runtime())->run
(
	function()
	{
		echo php_sapi_name().PHP_EOL;
		
		$function = function(){sleep(1);};
		\Swoole\Coroutine::create($function);
		
		
		echo "Finish";
	}
);
sleep(3);

echo $future->value();
$future->cancel();

webrobot1 avatar Jul 07 '23 15:07 webrobot1

Well as i see for use some function Swoole in Parallel you need call this function in main process (where you create Runtime classes).

But what about short-named function Swoole\Coroutine\run - it is never work in thread , but synonim global function "swoole_coroutine_create" work (ofcourse if it wal call in main process)

Well it is very stange things what Swoole not work in parallel thread "without dance with gitar"

webrobot1 avatar Jul 07 '23 17:07 webrobot1

If you can use parallel in swoole it will only be by accident.

Realistically, it appears as if the rest of the community have nothing to say about swoole, and I personally won't be testing there.

Sorry about that, but closing this issue now as there's no work for us to do here.

krakjoe avatar May 18 '24 18:05 krakjoe

Wanted to add a late note: one of the niceties of Swoole is its built-in Websocket/HTTP/etc servers and clients. Sometimes I've found that coroutines alone don't provide full async with streaming and timers, whereas using a separate thread appears to provide better scheduling.

zaunere avatar Aug 20 '24 06:08 zaunere