Comments (5)
I had the same idea to try to import aiorun
and if that fails, assuming that I run under pure asyncio
. But there is the edge case, that you have it installed, but still run the program under asyncio
, e.g. by not importing it in the first place, simulating that it is missing. (I know, I know, that's a messy way to check a program …)
Since I don't like global variables and the function initiating the closing program is only two classes deep, I pass an argument through to it. But I like your Singleton approach! I'll ponder to change to this.
from aiorun.
In most cases there is no difference because the actual event loop is the one in the standard library. I reckon if you're in a situation where you need to know you should probably just use the standard library's run()
: https://docs.python.org/3/library/asyncio-task.html#asyncio.run
This does nearly everything that aiorun does, except for the signal handlers and one or two other things.
from aiorun.
Thanks for your reply.
You guessed my scenario: I use aiorun
, if it is available and asyncio
, if aiorun
is not installed. In production run, I want to close the program from inside the loop when it receives a signal to do so (in my case via DBus), so with aiorun
I simply call loop.stop()
. Job done, thanks to your excellent aiorun
. With plain asyncio
the shutdown routine must be a bit more elaborated … Why do I not use asyncio
throughout, you might rightly ask? Because for the ordinary user my program is run in the background and for these users it is ok, to terminate it via the DBus. And frankly I do not care to respond to signals or terminate it with the keyboard in a clean way, because I am too lazy to program it. And for the few people starting it from the terminal or who are interested in other ways to terminate it, they can always install aiorun
. The program will detect it and use it. I am sure, there are better ways!!
Because it is not possible to detect, if the program was started under aiorun
's control, I simply use an argument to tell it, if aiorun
is available.
from aiorun.
Ah I see. I think your choice is fine. If aiorun simply being available is enough to know whether that's what you're using, then you could also make a utility function like
def using_aiorun() -> bool:
try:
import aiorun
except ImporError:
return False
else:
return True
But this will make things like tests more difficult if you want to test both with and without aiorun. I think your method of using an argument is fine. You could also instead set a global (or a singleton pattern, same difference) if you want to be able to easily tell from anywhere, without have to pass your argument all over the place.
from aiorun.
https://en.wikipedia.org/wiki/Singleton_pattern#Python_implementation
from aiorun.
Related Issues (20)
- Unhandled exception causes "Task exception was never retrieved" HOT 8
- 2019.11.1 aiorun version breaks aiologfields HOT 4
- README code sample is incorrect HOT 1
- Add ability for use run_until_complete HOT 12
- cancelling main starting coroutine firstly (before all other pending tasks) HOT 11
- Provided loop is not honored by set_event_loop_policy HOT 1
- Push latest master to the pypi HOT 4
- Dependabot couldn't authenticate with https://pypi.python.org/simple/
- Event loop shouldn't be stopped when the exception handler is invoked with no exception HOT 4
- aiorun 2020.12.1 does not seem to work together with aiohttp 3.7.4 HOT 6
- executor_workers, change to Optional[int]? HOT 2
- `shutdown_waits_for` example doesn't work as intended HOT 5
- SIGTERM, SIGINT seemingly ignored sometimes HOT 8
- `loop` in run function has wrong docstring HOT 4
- programatically stopping a loop HOT 4
- aiorun hangs in wait_for_cancelled_tasks HOT 9
- Replace flake8 with ruff in CI
- Error "If you provide a loop instance...", but I haven't HOT 3
- loop.create_task without saving a reference to the result HOT 7
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from aiorun.