dolejska-daniel/riot-api-league

Async not working

Closed this issue · 4 comments

Describe the bug
Hello.
I am using asynchronous ReactPHP in my application and would like to use your code to access to Riot API, but it seems it block I/O and is completely non-asynchronous despite the presence of methods.

To Reproduce

	$logger->info('before');
	
	$api
		->nextAsync(function($result) use($logger){
			$logger->info('success');
		}, function() use($logger){
			$logger->error('fail');
		})
		->getChampionRotations()
	;
	
	$logger->info('before commit');
	
	$api->commitAsync();
	
	$logger->info('after commit');

Expected behavior

[13.06 12:37:53][INFO] before
[13.06 12:37:53][INFO] before commit
[13.06 12:37:54][INFO] after commit
[13.06 12:37:54][INFO] success

Current behavior

[13.06 12:37:53][INFO] before
[13.06 12:37:53][INFO] before commit
[13.06 12:37:54][INFO] success
[13.06 12:37:54][INFO] after commit

Server:

  • PHP version: 7.4.14
  • react/react version: 1.1.0
  • dolejska-daniel/riot-api-league version: 1.0.1

Thanks.

Hello, thank you for your interest in this library! The commitAsync method is synchronous and always will be, that is the intended behaviour - PHP is not natively asynchronous hence what you're describing can never really occur (ReactPHP allows for event-driven programming to occur in PHP however the code doesn't run in parallel or asynchronously - or at least as far as I know, I'm not too familiar with ReactPHP).
However, you can use the event loop of ReactPHP to invoke methods kind of asynchronously. Check out this code snippet showing how to use ReactPHP's event loop within the Guzzle library. Furthermore, keep in mind that commitAsync will always block since that is when the request promises are being waited for.

So there are no plans to make the library non-blocking?

This is the least blocking it can be, I guess. You can make Guzzle use the ReactPHP's event loop and move the commitAsync to some "asynchronous" function called from the event loop too. That way, you can achieve some level of asynchronicity. However, PHP is not asynchronous; hence, true asynchronicity cannot be achieved - some function will always block.

I will do so. Blocking data loading takes most of the time. A lot of useful work can be done while waiting for a response from the Riot API. Thank you.