paulb@1 | 1 | #!/usr/bin/env python |
paulb@1 | 2 | |
paulb@1 | 3 | """ |
paulb@1 | 4 | A simple parallel processing API for Python, inspired somewhat by the thread |
paulb@1 | 5 | module, slightly less by pypar, and slightly less still by pypvm. |
paulb@1 | 6 | |
paulb@1 | 7 | To create new processes to run a function or any callable object, specify the |
paulb@1 | 8 | "callable" and any arguments as follows: |
paulb@1 | 9 | |
paulb@1 | 10 | channel = start(fn, arg1, arg2, named1=value1, named2=value2) |
paulb@1 | 11 | |
paulb@1 | 12 | This returns a channel which can then be used to communicate with the created |
paulb@1 | 13 | process. Meanwhile, in the created process, the given callable will be invoked |
paulb@1 | 14 | with another channel as its first argument followed by the specified arguments: |
paulb@1 | 15 | |
paulb@1 | 16 | def fn(channel, arg1, arg2, named1, named2): |
paulb@1 | 17 | # Read to and write from the channel. |
paulb@1 | 18 | # Return value is ignored. |
paulb@1 | 19 | ... |
paulb@1 | 20 | |
paulb@1 | 21 | To create new processes in a similar way to that employed when using os.fork |
paulb@1 | 22 | (ie. the fork system call on various operating systems), use the following |
paulb@1 | 23 | method: |
paulb@1 | 24 | |
paulb@1 | 25 | channel = create() |
paulb@1 | 26 | if channel.pid == 0: |
paulb@1 | 27 | # This code is run by the created process. |
paulb@1 | 28 | # Read to and write from the channel to communicate with the |
paulb@1 | 29 | # creating/calling process. |
paulb@1 | 30 | # An explicit exit of the process may be desirable to prevent the process |
paulb@1 | 31 | # from running code which is intended for the creating/calling process. |
paulb@1 | 32 | ... |
paulb@1 | 33 | else: |
paulb@1 | 34 | # This code is run by the creating/calling process. |
paulb@1 | 35 | # Read to and write from the channel to communicate with the created |
paulb@1 | 36 | # process. |
paulb@1 | 37 | ... |
paulb@1 | 38 | """ |
paulb@1 | 39 | |
paulb@1 | 40 | import os |
paulb@1 | 41 | import sys |
paulb@1 | 42 | |
paulb@1 | 43 | try: |
paulb@1 | 44 | import cPickle as pickle |
paulb@1 | 45 | except ImportError: |
paulb@1 | 46 | import pickle |
paulb@1 | 47 | |
paulb@1 | 48 | class Channel: |
paulb@1 | 49 | |
paulb@1 | 50 | "A communications channel." |
paulb@1 | 51 | |
paulb@1 | 52 | def __init__(self, pid, read_pipe, write_pipe): |
paulb@1 | 53 | |
paulb@1 | 54 | """ |
paulb@1 | 55 | Initialise the channel with a process identifier 'pid', a 'read_pipe' |
paulb@1 | 56 | from which messages will be received, and a 'write_pipe' into which |
paulb@1 | 57 | messages will be sent. |
paulb@1 | 58 | """ |
paulb@1 | 59 | |
paulb@1 | 60 | self.pid = pid |
paulb@1 | 61 | self.read_pipe = read_pipe |
paulb@1 | 62 | self.write_pipe = write_pipe |
paulb@1 | 63 | |
paulb@1 | 64 | def send(self, obj): |
paulb@1 | 65 | |
paulb@1 | 66 | "Send the given object 'obj' through the channel." |
paulb@1 | 67 | |
paulb@1 | 68 | pickle.dump(obj, self.write_pipe) |
paulb@1 | 69 | self.write_pipe.flush() |
paulb@1 | 70 | |
paulb@1 | 71 | def receive(self): |
paulb@1 | 72 | |
paulb@1 | 73 | "Receive an object through the channel, returning the object." |
paulb@1 | 74 | |
paulb@1 | 75 | obj = pickle.load(self.read_pipe) |
paulb@1 | 76 | if isinstance(obj, Exception): |
paulb@1 | 77 | raise obj |
paulb@1 | 78 | else: |
paulb@1 | 79 | return obj |
paulb@1 | 80 | |
paulb@1 | 81 | def create(): |
paulb@1 | 82 | |
paulb@1 | 83 | """ |
paulb@1 | 84 | Create a new process, returning a communications channel to both the |
paulb@1 | 85 | creating process and the created process. |
paulb@1 | 86 | """ |
paulb@1 | 87 | |
paulb@1 | 88 | parent_read_fd, child_write_fd = os.pipe() |
paulb@1 | 89 | child_read_fd, parent_write_fd = os.pipe() |
paulb@1 | 90 | |
paulb@1 | 91 | pid = os.fork() |
paulb@1 | 92 | if pid == 0: |
paulb@1 | 93 | return Channel(pid, os.fdopen(child_read_fd, "r"), os.fdopen(child_write_fd, "w")) |
paulb@1 | 94 | else: |
paulb@1 | 95 | return Channel(pid, os.fdopen(parent_read_fd, "r"), os.fdopen(parent_write_fd, "w")) |
paulb@1 | 96 | |
paulb@1 | 97 | def start(callable, *args, **kwargs): |
paulb@1 | 98 | |
paulb@1 | 99 | """ |
paulb@1 | 100 | Create a new process which shall start running in the given 'callable'. |
paulb@1 | 101 | Return a communications channel to the creating process, and supply such a |
paulb@1 | 102 | channel to the created process as the 'channel' parameter in the given |
paulb@1 | 103 | 'callable'. Additional arguments to the 'callable' can be given as |
paulb@1 | 104 | additional arguments to this function. |
paulb@1 | 105 | """ |
paulb@1 | 106 | |
paulb@1 | 107 | channel = create() |
paulb@1 | 108 | if channel.pid == 0: |
paulb@1 | 109 | try: |
paulb@1 | 110 | try: |
paulb@1 | 111 | callable(channel, *args, **kwargs) |
paulb@1 | 112 | except: |
paulb@1 | 113 | exc_type, exc_value, exc_traceback = sys.exc_info() |
paulb@1 | 114 | channel.send(exc_value) |
paulb@1 | 115 | finally: |
paulb@1 | 116 | sys.exit(0) |
paulb@1 | 117 | else: |
paulb@1 | 118 | return channel |
paulb@1 | 119 | |
paulb@1 | 120 | # vim: tabstop=4 expandtab shiftwidth=4 |