Back to index

moin  1.9.0~rc2
Public Member Functions | Public Attributes | Static Public Attributes | Private Member Functions
MoinMoin.util.SubProcess.Popen Class Reference

List of all members.

Public Member Functions

def send_signal
def terminate
def send_signal
def terminate
def kill
def communicate

Public Attributes

 timeout

Static Public Attributes

 kill = terminate

Private Member Functions

def _communicate
def _communicate

Detailed Description

Definition at line 26 of file SubProcess.py.


Member Function Documentation

def MoinMoin.util.SubProcess.Popen._communicate (   self,
  input 
) [private]

Definition at line 93 of file SubProcess.py.

00093 
00094         def _communicate(self, input):
00095             stdout = None # Return
00096             stderr = None # Return
00097 
00098             if self.stdout:
00099                 stdout = []
00100                 stdout_thread = threading.Thread(target=self._readerthread,
00101                                                  args=(self.stdout, stdout))
00102                 stdout_thread.setDaemon(True)
00103                 stdout_thread.start()
00104             if self.stderr:
00105                 stderr = []
00106                 stderr_thread = threading.Thread(target=self._readerthread,
00107                                                  args=(self.stderr, stderr))
00108                 stderr_thread.setDaemon(True)
00109                 stderr_thread.start()
00110 
00111             if self.stdin:
00112                 if input is not None:
00113                     self.stdin.write(input)
00114                 self.stdin.close()
00115 
00116             if self.stdout:
00117                 stdout_thread.join(self.timeout)
00118             if self.stderr:
00119                 stderr_thread.join(self.timeout)
00120 
00121             # if the threads are still alive, that means the thread join timed out
00122             timed_out = (self.stdout and self.stdout_thread.isAlive() or
00123                          self.stderr and self.stderr_thread.isAlive())
00124             if timed_out:
00125                 self.kill()
00126             else:
00127                 self.wait()
00128 
00129             # All data exchanged.  Translate lists into strings.
00130             if stdout is not None:
00131                 stdout = stdout[0]
00132             if stderr is not None:
00133                 stderr = stderr[0]
00134 
00135             # Translate newlines, if requested.  We cannot let the file
00136             # object do the translation: It is based on stdio, which is
00137             # impossible to combine with select (unless forcing no
00138             # buffering).
00139             if self.universal_newlines and hasattr(file, 'newlines'):
00140                 if stdout:
00141                     stdout = self._translate_newlines(stdout)
00142                 if stderr:
00143                     stderr = self._translate_newlines(stderr)
00144 
00145             return (stdout, stderr)

Here is the caller graph for this function:

def MoinMoin.util.SubProcess.Popen._communicate (   self,
  input 
) [private]

Definition at line 147 of file SubProcess.py.

00147 
00148         def _communicate(self, input):
00149             timed_out = False
00150             read_set = []
00151             write_set = []
00152             stdout = None # Return
00153             stderr = None # Return
00154 
00155             if self.stdin:
00156                 # Flush stdio buffer.  This might block, if the user has
00157                 # been writing to .stdin in an uncontrolled fashion.
00158                 self.stdin.flush()
00159                 if input:
00160                     write_set.append(self.stdin)
00161                 else:
00162                     self.stdin.close()
00163             if self.stdout:
00164                 read_set.append(self.stdout)
00165                 stdout = []
00166             if self.stderr:
00167                 read_set.append(self.stderr)
00168                 stderr = []
00169 
00170             input_offset = 0
00171             while read_set or write_set:
00172                 try:
00173                     rlist, wlist, xlist = select.select(read_set, write_set, [], self.timeout)
00174                 except select.error, e:
00175                     if e.args[0] == errno.EINTR:
00176                         continue
00177                     raise
00178 
00179                 timed_out = not (rlist or wlist or xlist)
00180                 if timed_out:
00181                     break
00182 
00183                 if self.stdin in wlist:
00184                     # When select has indicated that the file is writable,
00185                     # we can write up to PIPE_BUF bytes without risk
00186                     # blocking.  POSIX defines PIPE_BUF >= 512
00187                     chunk = input[input_offset:input_offset + 512]
00188                     bytes_written = os.write(self.stdin.fileno(), chunk)
00189                     input_offset += bytes_written
00190                     if input_offset >= len(input):
00191                         self.stdin.close()
00192                         write_set.remove(self.stdin)
00193 
00194                 if self.stdout in rlist:
00195                     data = os.read(self.stdout.fileno(), 1024)
00196                     if data == "":
00197                         self.stdout.close()
00198                         read_set.remove(self.stdout)
00199                     stdout.append(data)
00200 
00201                 if self.stderr in rlist:
00202                     data = os.read(self.stderr.fileno(), 1024)
00203                     if data == "":
00204                         self.stderr.close()
00205                         read_set.remove(self.stderr)
00206                     stderr.append(data)
00207 
00208             # All data exchanged.  Translate lists into strings.
00209             if stdout is not None:
00210                 stdout = ''.join(stdout)
00211             if stderr is not None:
00212                 stderr = ''.join(stderr)
00213 
00214             # Translate newlines, if requested.  We cannot let the file
00215             # object do the translation: It is based on stdio, which is
00216             # impossible to combine with select (unless forcing no
00217             # buffering).
00218             if self.universal_newlines and hasattr(file, 'newlines'):
00219                 if stdout:
00220                     stdout = self._translate_newlines(stdout)
00221                 if stderr:
00222                     stderr = self._translate_newlines(stderr)
00223 
00224             if timed_out:
00225                 self.kill()
00226             else:
00227                 self.wait()
00228             return (stdout, stderr)
00229 

Here is the call graph for this function:

def MoinMoin.util.SubProcess.Popen.communicate (   self,
  input = None,
  timeout = None 
)
Interact with process: Send data to stdin.  Read data from
stdout and stderr, until end-of-file is reached.  Wait for
process to terminate.  The optional input argument should be a
string to be sent to the child process, or None, if no data
should be sent to the child.

communicate() returns a tuple (stdout, stderr).

Definition at line 61 of file SubProcess.py.

00061 
00062     def communicate(self, input=None, timeout=None):
00063         """Interact with process: Send data to stdin.  Read data from
00064         stdout and stderr, until end-of-file is reached.  Wait for
00065         process to terminate.  The optional input argument should be a
00066         string to be sent to the child process, or None, if no data
00067         should be sent to the child.
00068 
00069         communicate() returns a tuple (stdout, stderr)."""
00070 
00071         self.timeout = timeout
00072 
00073         # Optimization: If we are only using one pipe, or no pipe at
00074         # all, using select() or threads is unnecessary.
00075         if [self.stdin, self.stdout, self.stderr].count(None) >= 2:
00076             stdout = None
00077             stderr = None
00078             if self.stdin:
00079                 if input:
00080                     self._fo_write_no_intr(self.stdin, input)
00081                 self.stdin.close()
00082             elif self.stdout:
00083                 stdout = self._fo_read_no_intr(self.stdout)
00084                 self.stdout.close()
00085             elif self.stderr:
00086                 stderr = self._fo_read_no_intr(self.stderr)
00087                 self.stderr.close()
00088             self.wait()
00089             return (stdout, stderr)
00090 
00091         return self._communicate(input)

Kill the process with SIGKILL

Definition at line 56 of file SubProcess.py.

00056 
00057         def kill(self):
00058             """Kill the process with SIGKILL
00059             """
00060             self.send_signal(signal.SIGKILL)

Here is the call graph for this function:

def MoinMoin.util.SubProcess.Popen.send_signal (   self,
  sig 
)
Send a signal to the process

Definition at line 30 of file SubProcess.py.

00030 
00031         def send_signal(self, sig):
00032             """Send a signal to the process
00033             """
00034             if sig == signal.SIGTERM:
00035                 self.terminate()
00036             else:
00037                 raise ValueError("Only SIGTERM is supported on Windows")

Here is the call graph for this function:

Here is the caller graph for this function:

def MoinMoin.util.SubProcess.Popen.send_signal (   self,
  sig 
)
Send a signal to the process

Definition at line 46 of file SubProcess.py.

00046 
00047         def send_signal(self, sig):
00048             """Send a signal to the process
00049             """
00050             os.kill(self.pid, sig)

Here is the call graph for this function:

Terminates the process

Definition at line 38 of file SubProcess.py.

00038 
00039         def terminate(self):
00040             """Terminates the process
00041             """
00042             TerminateProcess(self._handle, 1)

Here is the caller graph for this function:

Terminate the process with SIGTERM

Definition at line 51 of file SubProcess.py.

00051 
00052         def terminate(self):
00053             """Terminate the process with SIGTERM
00054             """
00055             self.send_signal(signal.SIGTERM)

Here is the call graph for this function:


Member Data Documentation

Definition at line 43 of file SubProcess.py.

Definition at line 70 of file SubProcess.py.


The documentation for this class was generated from the following file: