comparison python/multiproc.py @ 607:567492ef7b57

STUB: python/multiproc.py
author Jeff Hammel <k0scist@gmail.com>
date Sun, 02 Feb 2014 11:16:51 -0800
parents 5b561c7b2005
children 9a5cdd49af53
comparison
equal deleted inserted replaced
606:5b561c7b2005 607:567492ef7b57
25 # setup arguments 25 # setup arguments
26 self.command = command 26 self.command = command
27 _kwargs = self.defaults.copy() 27 _kwargs = self.defaults.copy()
28 _kwargs.update(kwargs) 28 _kwargs.update(kwargs)
29 29
30 # on unix, ``shell={True|False}`` should always come from the
31 # type of command (string or list)
32 if not mswindows:
33 _kwargs['shell'] = isinstance(command, string)
34
30 # output buffer 35 # output buffer
31 self.output_buffer = tempfile.SpooledTemporaryFile() 36 self.output_buffer = tempfile.SpooledTemporaryFile()
32 self.location = 0 37 self.location = 0
33 self.output = [] 38 self.output = []
34 _kwargs['stdout'] = self.output_buffer 39 _kwargs['stdout'] = self.output_buffer
35 40
36 # launch subprocess 41 # launch subprocess
37 self.start = time.time() 42 self.start = time.time()
38 subprocess.Popen.__init__(self, *args, **_kwargs) 43 subprocess.Popen.__init__(self, command, **_kwargs)
39 44
40 def wait(self, maxtime=None, sleep=1.): 45 def wait(self, maxtime=None, sleep=1.):
41 """ 46 """
42 maxtime -- timeout in seconds 47 maxtime -- timeout in seconds
43 sleep -- number of seconds to sleep between polling 48 sleep -- number of seconds to sleep between polling
49 run_time = curr_time - self.start 54 run_time = curr_time - self.start
50 if run_time > maxtime: 55 if run_time > maxtime:
51 # TODO: read from output 56 # TODO: read from output
52 return process.kill() 57 return process.kill()
53 58
59 # read from output buffer
60 self.output_buffer.seek(self.location)
61 read = self.output_buffer.read()
62 self.location += len(read)
63
54 # naptime 64 # naptime
55 if sleep: 65 if sleep:
56 time.sleep(sleep) 66 time.sleep(sleep)
57 67
58 # read from output buffer
59 self.output_buffer.seek(self.location)
60 read = self.output_buffer.read()
61 self.location += len(read)
62 68
63 def commandline(self): 69 def commandline(self):
64 """returns string of command line""" 70 """returns string of command line"""
65 71
66 if isinstance(self.command, string): 72 if isinstance(self.command, string):