From f44927234501d3e3c5be31d944387ae9e6168f08 Mon Sep 17 00:00:00 2001 From: Ryan Williams Date: Wed, 11 Nov 2009 03:45:48 -0800 Subject: [PATCH] Windows compatibility for tpool as requested by rtyler. Not yet tested on Windows! --- AUTHORS | 1 + eventlet/tpool.py | 38 ++++++++++++++++++++++++++++---------- 2 files changed, 29 insertions(+), 10 deletions(-) diff --git a/AUTHORS b/AUTHORS index 2f9e93a..2860b57 100644 --- a/AUTHORS +++ b/AUTHORS @@ -23,6 +23,7 @@ Linden Lab Contributors Thanks To --------- +* R. Tyler Ballance, bug report on tpool on Windows * Sergey Shepelev, PEP 8 police :-) * Luci Stanescu, for reporting twisted hub bug * Marcus Cavanaugh, for test case code that has been incredibly useful in tracking down bugs diff --git a/eventlet/tpool.py b/eventlet/tpool.py index b78842c..058d796 100644 --- a/eventlet/tpool.py +++ b/eventlet/tpool.py @@ -22,11 +22,11 @@ from eventlet import api, coros, greenio QUIET=False -_rpipe = _wpipe = _rfile = None +_rfile = _wfile = None def _signal_t2e(): from eventlet import util - nwritten = util.__original_write__(_wpipe, ' ') + sent = util.__original_write__(_wfile.fileno(), ' ') _reqq = Queue(maxsize=-1) _rspq = Queue(maxsize=-1) @@ -35,7 +35,7 @@ def tpool_trampoline(): global _reqq, _rspq while(True): try: - _c = _rfile.recv(1) + _c = _rfile.read(1) except ValueError: break # will be raised when pipe is closed assert(_c != "") @@ -173,16 +173,33 @@ _threads = {} _coro = None _setup_already = False def setup(): - global _rpipe, _wpipe, _rfile, _threads, _coro, _setup_already + global _rfile, _wfile, _threads, _coro, _setup_already if _setup_already: return else: _setup_already = True - _rpipe, _wpipe = os.pipe() - _rfile = os.fdopen(_rpipe,"r",0) - ## Work whether or not wrap_pipe_with_coroutine_pipe was called - if not isinstance(_rfile, greenio.GreenPipe): - _rfile = greenio.GreenPipe(_rfile) + try: + _rpipe, _wpipe = os.pipe() + _wfile = os.fdopen(_wpipe,"w",0) + _rfile = os.fdopen(_rpipe,"r",0) + ## Work whether or not wrap_pipe_with_coroutine_pipe was called + if not isinstance(_rfile, greenio.GreenPipe): + _rfile = greenio.GreenPipe(_rfile) + except ImportError: + # This is Windows compatibility -- use a socket instead of a pipe because + # pipes don't really exist on Windows. + import socket + from eventlet import util + sock = util.__original_socket__(socket.AF_INET, socket.SOCK_STREAM) + sock.bind(('localhost', 0)) + sock.listen(50) + csock = util.__original_socket__(socket.AF_INET, socket.SOCK_STREAM) + csock.connect(('localhost', sock.getsockname()[1])) + csock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) + nsock, addr = sock.accept() + nsock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) + _rfile = greenio.GreenFile(greenio.GreenSocket(csock)) + _wfile = nsock.makefile() for i in range(0,_nthreads): _threads[i] = threading.Thread(target=tworker) @@ -199,6 +216,7 @@ def killall(): _reqq.put(None) for thr in _threads.values(): thr.join() - _rfile.close() api.kill(_coro) + _rfile.close() + _wfile.close() _setup_already = False