FabLabKasse.libs.process_helper package

Submodules

FabLabKasse.libs.process_helper.asyncproc module

class FabLabKasse.libs.process_helper.asyncproc.Process(*params, **kwparams)[source]

Bases: object

Manager for an asynchronous process. The process will be run in the background, and its standard output and standard error will be collected asynchronously.

Since the collection of output happens asynchronously (handled by threads), the process won’t block even if it outputs large amounts of data and you do not call Process.read*().

Similarly, it is possible to send data to the standard input of the process using the write() method, and the caller of write() won’t block even if the process does not drain its input.

On the other hand, this can consume large amounts of memory, potentially even exhausting all memory available.

Parameters are identical to subprocess.Popen(), except that stdin, stdout and stderr default to subprocess.PIPE instead of to None. Note that if you set stdout or stderr to anything but PIPE, the Process object won’t collect that output, and the read*() methods will always return empty strings. Also, setting stdin to something other than PIPE will make the write() method raise an exception.

closeinput()[source]

Close the standard input of a process, so it receives EOF.

kill(signal)[source]

Send a signal to the process. Raises OSError, with errno set to ECHILD, if the process is no longer running.

pid()[source]

Return the process id of the process. Note that if the process has died (and successfully been waited for), that process id may have been re-used by the operating system.

read()[source]

Read data written by the process to its standard output.

readboth()[source]

Read data written by the process to its standard output and error. Return value is a two-tuple ( stdout-data, stderr-data ).

WARNING! The name of this method is ugly, and may change in future versions!

readerr()[source]

Read data written by the process to its standard error.

terminate(graceperiod=1)[source]

Terminate the process, with escalating force as needed. First try gently, but increase the force if it doesn’t respond to persuassion. The levels tried are, in order:

  • close the standard input of the process, so it gets an EOF.
  • send SIGTERM to the process.
  • send SIGKILL to the process.

terminate() waits up to GRACEPERIOD seconds (default 1) before escalating the level of force. As there are three levels, a total of (3-1)*GRACEPERIOD is allowed before the process is SIGKILL:ed. GRACEPERIOD must be an integer, and must be at least 1.

If the process was started with stdin not set to PIPE, the first level (closing stdin) is skipped.

wait(flags=0)[source]

Return the process’ termination status.

If bitmask parameter ‘flags’ contains os.WNOHANG, wait() will return None if the process hasn’t terminated. Otherwise it will wait until the process dies.

It is permitted to call wait() several times, even after it has succeeded; the Process instance will remember the exit status from the first successful call, and return that on subsequent calls.

write(data)[source]

Send data to a process’s standard input.

FabLabKasse.libs.process_helper.asyncproc.with_timeout(timeout, func, *args, **kwargs)[source]

Call a function, allowing it only to take a certain amount of time.

param timeout:The time, in seconds, the function is allowed to spend. This must be an integer, due to limitations in the SIGALRM handling.
param func:The function to call.
param *args:Non-keyword arguments to pass to func.
param **kwargs:Keyword arguments to pass to func.

Upon successful completion, with_timeout() returns the return value from func. If a timeout occurs, the Timeout exception will be raised.

If an alarm is pending when with_timeout() is called, with_timeout() tries to restore that alarm as well as possible, and call the SIGALRM signal handler if it would have expired during the execution of func. This may cause that signal handler to be executed later than it would normally do. In particular, calling with_timeout() from within a with_timeout() call with a shorter timeout, won’t interrupt the inner call. I.e., with_timeout(5, with_timeout, 60, time.sleep, 120)` won’t interrupt the time.sleep() call until after 60 seconds.

exception FabLabKasse.libs.process_helper.asyncproc.Timeout[source]

Bases: exceptions.Exception

Exception raised by with_timeout() when the operation takes too long.

FabLabKasse.libs.process_helper.nonblockingProcess module

FabLabKasse.libs.process_helper.nonblockingProcess.demo()[source]

small example that communicates with bc, the commandline calculator. Note that readline() never blocks!

class FabLabKasse.libs.process_helper.nonblockingProcess.nonblockingProcess(cmd, env=None)[source]

Bases: object

non-blocking subprocess, based on asyncproc

hasLine()[source]
isAlive()[source]
readline()[source]

read line from process stdout, if available, else return None

write(string)[source]

Module contents