News for hackers. Stuff that matters.
Re:Exactly-Once Initialization in Asynchronous Python by alexchamberlain on Saturday August 01, 2020 @02:56AM (#24018292)
I'd just call the function once by avoiding the global; construct your database access object at the start of your asynchronous main method and dependency inject it to other tasks.
Re:Exactly-Once Initialization in Asynchronous Python by dilatedmind on Saturday August 01, 2020 @10:07AM (#24020519)
His asyncpg example doesn't make much sense to me. What if there was a config change with a bad password? I would like to know this immediately on startup, else my rolling deploy is going to bring down all the previously well configured instances, and by the time we lazily try to connect to postgres it's too late.

I'm not a big python user, but I do find it kind of surprising there isn't an awaitable and thread safe mutex in the stdlib.

Re:Exactly-Once Initialization in Asynchronous Python by dtech on Saturday August 01, 2020 @10:59AM (#24020918)
Python is highly opinionated towards single-threadeding, and the infamous GIL makes "true" multi-threading hard. Async and multi-threading has only recently gotten some love at all in the language and stdlib.

I'd say single-threading is the right call for 99% of Python's use-cases and users. C extensions are available and widely used for core functionality that needs to be fast and/or parallel, e.g. large parts of numpy are in the c extension.

Re:Exactly-Once Initialization in Asynchronous Python by orf on Saturday August 01, 2020 @06:01AM (#24019235)
This. Your code often becomes easier to read and test as well.
Re:Exactly-Once Initialization in Asynchronous Python by np_tedious on Saturday August 01, 2020 @07:24AM (#24019608)
Can you clarify what you mean by dependency injection in python? Did you mean a DI framework or something more informal?

I've seen DI frameworks in python but not really used them. At a glance they don't strike me as pythonic. Rolling your own kind of inversion of control can result in unruly "config" or "context" objects that bring difficulties as well.

Re:Exactly-Once Initialization in Asynchronous Python by fulafel on Saturday August 01, 2020 @08:42AM (#24020027)
DI is just a convoluted synonym for passing in arguments.
Re:Exactly-Once Initialization in Asynchronous Python by alexchamberlain on Sunday August 02, 2020 @03:46PM (#24031097)
Indeed. If you want it to be cleaner, a discipline of constructor arguments for injection and __call__ arguments for semantic input is really all you need.
Re:Exactly-Once Initialization in Asynchronous Python by oweiler on Saturday August 01, 2020 @07:40AM (#24019685)
Pass the fully constructed object as parameter. No need for a DI framework.
Re:Exactly-Once Initialization in Asynchronous Python by idclip on Saturday August 01, 2020 @07:45AM (#24019710)
I must say i havnt done any dependency injection in python. Did so in scala and JS though.

So i assume he means initilize it once in program entry and Pass in the pointer to the object that needs it via including it the constructor or a function call.

Do you think There’s a pythonic way of doing these things?

Re:Exactly-Once Initialization in Asynchronous Python by danielscrubs on Saturday August 01, 2020 @07:31AM (#24019638)
So you’d block the thread and call it a day?