Giter VIP home page Giter VIP logo

Comments (5)

sphh avatar sphh commented on May 29, 2024 1

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.

cjrh avatar cjrh commented on May 29, 2024

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.

sphh avatar sphh commented on May 29, 2024

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.

cjrh avatar cjrh commented on May 29, 2024

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.

cjrh avatar cjrh commented on May 29, 2024

https://en.wikipedia.org/wiki/Singleton_pattern#Python_implementation

from aiorun.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.