[Python编程(第4版)].(Programming.Python.4th.Edition).Mark.Lutz.文字版

(yzsuai) #1
socket.error: [Errno 10038] An operation was attempted on something that is not
a socket

Recall from Chapter 5 that on Windows multiprocessing passes context to a new
Python interpreter process by pickling it, and that Process arguments must all be
pickleable for Windows. Sockets in Python 3.1 don’t trigger errors when pickled thanks
to the class they are an instance of, but they are not really pickled correctly:


>>> from pickle import *
>>> from socket import *
>>> s = socket()
>>> x = dumps(s)
>>> s
<socket.socket object, fd=180, family=2, type=1, proto=0>
>>> loads(x)
<socket.socket object, fd=-1, family=0, type=0, proto=0>
>>> x
b'\x80\x03csocket\nsocket\nq\x00)\x81q\x01N}q\x02(X\x08\x00\x00\x00_io_refsq\x03
K\x00X\x07\x00\x00\x00_closedq\x04\x89u\x86q\x05b.'

As we saw in Chapter 5, multiprocessing has other IPC tools such as its own pipes and
queues that might be used instead of sockets to work around this issue, but clients
would then have to use them, too—the resulting server would not be as broadly ac-
cessible as one based upon general Internet sockets.


Even if multiprocessing did work on Windows, though, its need to start a new Python
interpreter would likely make it much slower than the more traditional technique of
spawning threads to talk to clients. Coincidentally, that brings us to our next topic.


Threading Servers


The forking model just described works well on Unix-like platforms in general, but it
suffers from some potentially significant limitations:


Performance
On some machines, starting a new process can be fairly expensive in terms of time
and space resources.


Portability
Forking processes is a Unix technique; as we’ve learned, the os.fork call currently
doesn’t work on non-Unix platforms such as Windows under standard Python. As
we’ve also learned, forks can be used in the Cygwin version of Python on Windows,
but they may be inefficient and not exactly the same as Unix forks. And as we just
discovered, multiprocessing won’t help on Windows, because connected sockets
are not pickleable across process boundaries.


Complexity
If you think that forking servers can be complicated, you’re not alone. As we just
saw, forking also brings with it all the shenanigans of managing and reaping zom-
bies—cleaning up after child processes that live shorter lives than their parents.


Handling Multiple Clients | 815
Free download pdf