3 from types import FunctionType
6 from eventlet import greenio
7 from eventlet import patcher
8 from eventlet.green import select
9 from eventlet.support import six
12 patcher.inject('subprocess', globals(), ('select', select))
13 subprocess_orig = __import__("subprocess")
16 if getattr(subprocess_orig, 'TimeoutExpired', None) is None:
17 # Backported from Python 3.3.
18 # https://bitbucket.org/eventlet/eventlet/issue/89
19 class TimeoutExpired(Exception):
20 """This exception is raised when the timeout expires while waiting for
24 def __init__(self, cmd, timeout, output=None):
26 self.timeout = timeout
30 return ("Command '%s' timed out after %s seconds" %
31 (self.cmd, self.timeout))
34 # This is the meat of this module, the green version of Popen.
35 class Popen(subprocess_orig.Popen):
36 """eventlet-friendly version of subprocess.Popen"""
37 # We do not believe that Windows pipes support non-blocking I/O. At least,
38 # the Python file objects stored on our base-class object have no
39 # setblocking() method, and the Python fcntl module doesn't exist on
40 # Windows. (see eventlet.greenio.set_nonblocking()) As the sole purpose of
41 # this __init__() override is to wrap the pipes for eventlet-friendly
42 # non-blocking I/O, don't even bother overriding it on Windows.
43 if not subprocess_orig.mswindows:
44 def __init__(self, args, bufsize=0, *argss, **kwds):
46 # Forward the call to base-class constructor
47 subprocess_orig.Popen.__init__(self, args, 0, *argss, **kwds)
48 # Now wrap the pipes, if any. This logic is loosely borrowed from
49 # eventlet.processes.Process.run() method.
50 for attr in "stdin", "stdout", "stderr":
51 pipe = getattr(self, attr)
52 if pipe is not None and not type(pipe) == greenio.GreenPipe:
53 wrapped_pipe = greenio.GreenPipe(pipe, pipe.mode, bufsize)
54 setattr(self, attr, wrapped_pipe)
55 __init__.__doc__ = subprocess_orig.Popen.__init__.__doc__
57 def wait(self, timeout=None, check_interval=0.01):
58 # Instead of a blocking OS call, this version of wait() uses logic
59 # borrowed from the eventlet 0.2 processes.Process.wait() method.
60 if timeout is not None:
61 endtime = time.time() + timeout
65 if status is not None:
67 if timeout is not None and time.time() > endtime:
68 raise TimeoutExpired(self.args, timeout)
69 eventlet.sleep(check_interval)
71 if e.errno == errno.ECHILD:
72 # no child process, this happens if the child process
73 # already died and has been cleaned up
77 wait.__doc__ = subprocess_orig.Popen.wait.__doc__
79 if not subprocess_orig.mswindows:
80 # don't want to rewrite the original _communicate() method, we
81 # just want a version that uses eventlet.green.select.select()
82 # instead of select.select().
83 _communicate = FunctionType(
84 six.get_function_code(six.get_unbound_function(
85 subprocess_orig.Popen._communicate)),
88 _communicate_with_select = FunctionType(
89 six.get_function_code(six.get_unbound_function(
90 subprocess_orig.Popen._communicate_with_select)),
92 _communicate_with_poll = FunctionType(
93 six.get_function_code(six.get_unbound_function(
94 subprocess_orig.Popen._communicate_with_poll)),
96 except AttributeError:
99 # Borrow subprocess.call() and check_call(), but patch them so they reference
100 # OUR Popen class rather than subprocess.Popen.
101 call = FunctionType(six.get_function_code(subprocess_orig.call), globals())
102 check_call = FunctionType(six.get_function_code(subprocess_orig.check_call), globals())