Summary of threading for experienced non-Python programmers?
hniksic at xemacs.org
Sun Mar 30 23:24:56 CEST 2008
Hrvoje Niksic <hniksic at xemacs.org> writes:
> I believe you stil misunderstand. The select module doesn't provide
> an inteface to aio(3). It provides an interface to select() and
> poll() system calls, which don't provide asynchronous access to
> regular files.
It occurred to me that I didn't provide an example of what I mean by
select not providing asynchronous access to regular files. Using this
>>> import os, select
>>> fd = os.open('/mnt/custom/x.jpg', os.O_RDONLY)
# /mnt/custom is an SMB or NFS mount. At this point, kill -STOP the
# SMB server.
>>> select.select([fd], , , 10) # test whether fd is readable
(, , ) # exits immediately, assume
>>> os.read(3, 1024)
... hangs until smb server continues ...
The thing is, select() *always* returns immediately on regular files,
even if they are in fact not readable or writable. In this case,
select() claimed the file descriptor to be readable when in fact it
wasn't. The same is the case with poll, but not with aio.
In most cases this isn't a problem, but it does mean that an
application that reads from a network-share-located file in a
select/poll-driven event loop will stall until the file server
responds. Threads, on the other hand, don't have this problem. A
program that reads the file in a separate thread will not block even
if the file is on a temporarily non-responding NFS server.
More information about the Python-list