Autostart the scheduler
mbonneau opened this issue · comments
This issue is to discuss using register_shutdown_function
to automatically start the loop. This would be done bootstrap.php and set within require
in composer.json, like this:
register_shutdown_function(function () {
Loop::execute(function () {}, Loop::get());
});
Pros:
- Users will not need to wrap their code in
Loop::execute
. - Currently, if someone tries using operators and forgets to wrap them, the program will exit and nothing will happen. By using register shutdown, everything that has been sent to the loop before the program exists, will run.
- It hides more of the internal plumbing, that shouldn't matter to most users
Cons:
???
Perhaps too much magic? Would this be up to the event loop implementations?
This really is helpful mainly to those who are not familiar with async in PHP. For people who use it everyday the idea of having to start your event loop is second nature. People who are used to node or async in-browser are going to wonder why their beautiful Rx structures do nothing.
Yes, from that perspective it makes sense. I'm just a little worried we will be hiding too much. The shutdown function also doesn't play nice with things like custom exception handlers AFAIK.
The magic scheduler is nice though.
@asm89 @davidwdan - This is really of no consequence to me (it is more to make Rx easier for new people). Is this something we can just wait until after a v2 release to discuss?
Also, if loop implementations auto-start, that would work just as well.
This implementation of bootstrap.php
expects you to register a DriverFactory
somewhere in your code. Otherwise this will throw an exception.
Package wyrihaximus/react-async-interop-loop
has its own bootstrap.php
that registers its DriverFactory
but is this reliable? I couldn't find anywhere that packages providing async-interop/event-loop-implementation
have to register loop drivers.
I'm 50/50 on this. Like stated above, it's easy enough to provide this functionality by other means.
@martinsik It's not part of the spec, but I believe all of the other implementations are doing something similar. Since we require a loop implementation, is it safe to assume that a driver has to be set? If so, I don't see a problem with it throwing an exception.
We should close this issue as it poses no real concern for most users of async and really is more in the realm of the event-loop library or as @davidwdan can be provided in a helper library.
I would like to see the bootstrap.php file moved from the src into test as it is not intended for general use.