4 Executes python functions and their arguements as separate processes and returns their return values through pickling. This modules offers a single function:
6 Function: split_all(funcs)
8 The function returns a generator objects that allowes iteration over the results,
12 funcs: a list of tuples (f, args) where f is a python function and args is a collection of arguments for f.
16 1. Global variables in the parent process are not affected by the child processes.
17 2. The functions can only return simple types, see the pickle module for details
21 Assume you would like to run the function f_1(1), f_2(1,2), f_3(1,2,3) in different processes.
32 Construct a tuple of the function and arguments for each function
38 Create a list containing these tuples:
40 funcs = [t_1, t_2, t_3]
42 Use the function split_all() to run these functions in separate processes:
44 for res in split_all(funcs):
53 (The order may be different, except that in this case the processes are so fast that they terminate before the next one is created)
55 Alternatively, you may quite in the middle, say after the first process returns:
57 for res in split_all(funcs):
61 This will kill all processes not yet finished.
63 To run ABC operations, that required saving the child process state and restoring it at the parent, use abc_split_all().
69 print "pid=%d, abc_f(%s)"%(os.getpid(), truth)
70 pyabc.run_command('read_truth %s'%truth)
71 pyabc.run_command('strash')
78 for _ in abc_split_all(funcs):
79 pyabc.run_command('write_verilog /dev/stdout')
81 Author: Baruch Sterin <sterin@berkeley.edu>
89 import cPickle
as pickle
95 from contextlib
import contextmanager
102 rrdy,_,_ = select.select(rlist,[],[])
105 except select.error
as e:
106 if e[0] == errno.EINTR:
119 return len(self.
fds) == 0
129 i, fd = self.
fds[pid]
140 os.kill( pid, signal.SIGINT)
141 except Exception
as e:
142 print >>sys.stderr,
'exception while trying to kill pid=%d: '%pid, e
151 self.
_kill( [ self.
pids[i]
for i
in ids ] )
154 self.
_kill( self.fds.keys() )
162 traceback.print_exc()
166 with os.fdopen( fdw,
"w" )
as fout:
167 pickle.dump(res, fout)
177 fcntl.fcntl(pr, fcntl.F_SETFL, os.O_NONBLOCK)
179 parentpid = os.getpid()
190 pyabc.close_on_fork(pw)
192 rc = self.
child( pw, f)
200 if os.getpid() != parentpid:
206 self.pids.append(pid)
207 self.
fds[pid] = (i, fd)
208 self.
buffers[fd] = cStringIO.StringIO()
212 return [ self.
fork_one(f)
for f
in funcs ]
216 rlist = [ fd
for _, (_,fd)
in self.fds.iteritems() ]
217 rlist.append(pyabc.wait_fd)
227 if fd == pyabc.wait_fd:
231 self.
buffers[fd].write( os.read(fd, 16384) )
240 assert pid
in self.
fds
243 i, fd = self.
fds[pid]
255 s = os.read(fd, 16384)
263 return (i, pickle.loads(buffer.getvalue()))
264 except EOFError, pickle.UnpicklingError:
292 return lambda *args, **kwargs:
lambda : f(*args,**kwargs)
304 for suffix
in suffixes:
305 with tempfile.NamedTemporaryFile(delete=
False, suffix=suffix)
as file:
306 names.append( file.name )
314 with tempfile.NamedTemporaryFile(delete=
False, suffix=
'.aig')
as file:
316 with tempfile.NamedTemporaryFile(delete=
False, suffix=
'.log')
as file:
318 pyabc.run_command(
r'write_status %s'%self.
log)
319 pyabc.run_command(
r'write_aiger %s'%self.
aig)
322 os.unlink( self.
aig )
323 os.unlink( self.
log )
326 pyabc.run_command(
r'read_aiger %s'%self.
aig)
327 pyabc.run_command(
r'read_status %s'%self.
log)
332 def child(f, aig, log):
334 pyabc.run_command(
r'write_status %s'%log)
335 pyabc.run_command(
r'write_aiger %s'%aig)
338 def parent(res, aig, log):
339 pyabc.run_command(
r'read_aiger %s'%aig)
340 pyabc.run_command(
r'read_status %s'%log)
345 funcs = [
defer(child)(f, tmp[2*i],tmp[2*i+1])
for i,f
in enumerate(funcs) ]
348 yield i, parent(res, tmp[2*i],tmp[2*i+1])
350 if __name__ ==
"__main__":
361 return i*100+j*10+k+1
371 funcs = [t_1, t_2, t_3]
390 print "pid=%d, abc_f(%s)"%(os.getpid(), truth)
391 pyabc.run_command(
'read_truth %s'%truth)
392 pyabc.run_command(
'strash')
396 defer(abc_f)(
"1000"),
407 pyabc.run_command(
'write_verilog /dev/stdout')
412 pyabc.run_command(
'write_verilog /dev/stdout')