[Python-Dev] threadsafe patch for asynchat
Josiah Carlson
jcarlson at uci.edu
Thu Feb 9 06:02:59 CET 2006
Mark Edgington <edgimar at lycos.com> wrote:
>
> Martin v. Löwis wrote:
> > That patch looks wrong. What does it mean to "run in a thread"?
> > All code runs in a thread, all the time: sometime, that thread
> > is the main thread.
> >
> > Furthermore, I can't see any presumed thread-unsafety in asynchat.
>
> Ok, perhaps the notation could be improved, but the idea of the
> semaphore in the patch is "Does it run inside of a multithreaded
> environment, and could its push() functions be called from a different
> thread?"
Asyncore is not threadsafe. The reason it is not threadsafe is because
there was no effort made to make it threadsafe, because it is not
uncommon for the idea of asynchronous sockets to be the antithesis of
threaded socket servers.
In any case, one must be very careful as (at least in older versions of
Python on certain platforms), running sock.send(data) on two threads
simultaneously for the same socket was a segfault. I understand that
this is what you are trying to avoid, but have you considered just doing...
q = Queue.Queue()
def push(sock, data):
q.put((sock, data))
def mainloop():
...
while not q.empty():
sock, data = q.get()
sock.push(data)
...
Wow, now we don't have to update the standard library to introduce a
false sense of thread-safety into asyncore!
- Josiah
More information about the Python-Dev
mailing list