[Async-sig] new library: sniffio – Sniff out which async library your code is running under

Nathaniel Smith njs at pobox.com
Sat Aug 18 03:20:31 EDT 2018


On Fri, Aug 17, 2018 at 11:44 PM, Chris Jerdonek
<chris.jerdonek at gmail.com> wrote:
> On Fri, Aug 17, 2018 at 12:50 PM, Nathaniel Smith <njs at pobox.com> wrote:
>> On Fri, Aug 17, 2018, 12:12 Chris Jerdonek <chris.jerdonek at gmail.com> wrote:
>>>
>>> Did you also think about whether it would be possible for a library to
>>> advertise itself without having to depend on a third-party library
>>> (e.g. using some sort of convention)? That would permit a less
>>> "centralized" approach.
>>
>>
>> What kind of convention do you have in mind?
>
> Good question. I don't claim to know the answer which is why I asked
> if you had thought about it. The *kind* of thing I had in mind was to
> set a variable with an agreed-upon name and value on an agreed-upon
> module in the standard library -- though I agree that seems hacky as
> stated.
>
> It does seem to me like something that should (already?) have a
> general solution. What other ways does Python let things register or
> "announce" themselves?

Well, you could register an entry in an some global dict under an
agreed-on key, like, say, sys.modules["sniffio"]. Of course, whenever
you're mutating a global object like this you should worry about name
collisions, but fortunately that particular dict has a good convention
for reserving names. In fact there's a whole web service called "PyPI"
devoted to managing those registrations! And then you might as well
upload the code for accessing that variable to the web service, so
everyone doesn't have to copy/paste it into their programs... ;-)

Now that packaging works reliably, it's a pretty good solution for
this kind of thing IMHO.

-n

-- 
Nathaniel J. Smith -- https://vorpus.org


More information about the Async-sig mailing list