Threading support?

19 views
Skip to first unread message

Paul Hoffman

unread,
Oct 2, 2021, 12:15:00 PM10/2/21
to dnspyth...@googlegroups.com
Greetings again. A long time ago, I thought I remembered seeing that some parts of dnspython were not safe when using Python threading; I could certainly be mis-remembering this. I don't see threading mentioned in the current docs. Is dnspython 2.1 fully thread-safe? If not, which parts are known to not be safe?

Bob Halley

unread,
Oct 2, 2021, 3:37:03 PM10/2/21
to dnspython-users
Generally speaking, dnspython is not designed to be thread-safe, but it does provide thread-safe classes for cases where you're likely to want concurrency.

Names and rdata are immutable, and so safe to share between threads.

Container classes like rdatasets, rrsets, messages, and Zone (but not VersionedZone) are NOT thread-safe, and any concurrency must be managed by the application.

The resolver is also not thread-safe in terms of its configuration, but multiple threads can call a resolver's resolve() method as resolutions do not mutate the state of the resolver other than updating the cache (if one is in use).  The resolver cache implementations ARE thread-safe.  So basically you can do web-crawling and other things
concurrently.

The VersionedZone object is also thread-safe.  It can only be altered by transactions, transactions are locked, and committed data are immutable.

Paul Hoffman

unread,
Oct 2, 2021, 4:38:53 PM10/2/21
to Bob Halley, dnspython-users
Thanks, this is helpful! Is what you say about dns.resolver also true for dns.query? That is, can I send UDP and TCP queries on separate threads and assume that the responses will be correct and not step on each other?

--
You received this message because you are subscribed to the Google Groups "dnspython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dnspython-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/dnspython-users/8422a9e1-75a4-4c32-af35-3b248308357bn%40googlegroups.com.

Bob Halley

unread,
Oct 2, 2021, 8:18:07 PM10/2/21
to dnspython-users
The dns.query.* routines are safe to use from multiple threads except in the case where you are passing in a socket instead of letting it create one as it usually does.  In this case, it's on you to ensure thread safety to prevent interleaving of data and other confusions.  I don't know if requests sessions or SSL contexts are thread safe, but if not, then you'd again have to ensure safety yourself.
Reply all
Reply to author
Forward
0 new messages