paulb@40 | 1 | #!/usr/bin/env python |
paulb@40 | 2 | |
paulb@40 | 3 | """ |
paulb@40 | 4 | A simple parallel processing API for Python, inspired somewhat by the thread |
paulb@40 | 5 | module, slightly less by pypar, and slightly less still by pypvm. |
paulb@40 | 6 | |
paulb@67 | 7 | Copyright (C) 2005, 2006, 2007 Paul Boddie <paul@boddie.org.uk> |
paulb@41 | 8 | |
paulb@79 | 9 | This program is free software; you can redistribute it and/or modify it under |
paulb@79 | 10 | the terms of the GNU Lesser General Public License as published by the Free |
paulb@79 | 11 | Software Foundation; either version 3 of the License, or (at your option) any |
paulb@79 | 12 | later version. |
paulb@41 | 13 | |
paulb@79 | 14 | This program is distributed in the hope that it will be useful, but WITHOUT |
paulb@79 | 15 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paulb@79 | 16 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
paulb@79 | 17 | details. |
paulb@41 | 18 | |
paulb@79 | 19 | You should have received a copy of the GNU Lesser General Public License along |
paulb@79 | 20 | with this program. If not, see <http://www.gnu.org/licenses/>. |
paulb@41 | 21 | |
paulb@41 | 22 | -------- |
paulb@41 | 23 | |
paulb@67 | 24 | The recommended styles of programming using pprocess involve the "Thread-style |
paulb@67 | 25 | Processing" and "Convenient Message Exchanges" sections below, although |
paulb@67 | 26 | developers may wish to read the "Message Exchanges" section for more details of |
paulb@67 | 27 | the API concerned, and the "Fork-style Processing" section may be of interest to |
paulb@67 | 28 | those with experience of large scale parallel processing systems. |
paulb@67 | 29 | |
paulb@40 | 30 | Thread-style Processing |
paulb@40 | 31 | ----------------------- |
paulb@40 | 32 | |
paulb@40 | 33 | To create new processes to run a function or any callable object, specify the |
paulb@40 | 34 | "callable" and any arguments as follows: |
paulb@40 | 35 | |
paulb@79 | 36 | channel = pprocess.start(fn, arg1, arg2, named1=value1, named2=value2) |
paulb@40 | 37 | |
paulb@40 | 38 | This returns a channel which can then be used to communicate with the created |
paulb@40 | 39 | process. Meanwhile, in the created process, the given callable will be invoked |
paulb@40 | 40 | with another channel as its first argument followed by the specified arguments: |
paulb@40 | 41 | |
paulb@40 | 42 | def fn(channel, arg1, arg2, named1, named2): |
paulb@40 | 43 | # Read from and write to the channel. |
paulb@40 | 44 | # Return value is ignored. |
paulb@40 | 45 | ... |
paulb@40 | 46 | |
paulb@40 | 47 | Fork-style Processing |
paulb@40 | 48 | --------------------- |
paulb@40 | 49 | |
paulb@40 | 50 | To create new processes in a similar way to that employed when using os.fork |
paulb@40 | 51 | (ie. the fork system call on various operating systems), use the following |
paulb@40 | 52 | method: |
paulb@40 | 53 | |
paulb@40 | 54 | channel = create() |
paulb@40 | 55 | if channel.pid == 0: |
paulb@40 | 56 | # This code is run by the created process. |
paulb@40 | 57 | # Read from and write to the channel to communicate with the |
paulb@40 | 58 | # creating/calling process. |
paulb@40 | 59 | # An explicit exit of the process may be desirable to prevent the process |
paulb@40 | 60 | # from running code which is intended for the creating/calling process. |
paulb@40 | 61 | ... |
paulb@40 | 62 | else: |
paulb@40 | 63 | # This code is run by the creating/calling process. |
paulb@40 | 64 | # Read from and write to the channel to communicate with the created |
paulb@40 | 65 | # process. |
paulb@40 | 66 | ... |
paulb@40 | 67 | |
paulb@40 | 68 | Message Exchanges |
paulb@40 | 69 | ----------------- |
paulb@40 | 70 | |
paulb@40 | 71 | When creating many processes, each providing results for the consumption of the |
paulb@40 | 72 | main process, the collection of those results in an efficient fashion can be |
paulb@40 | 73 | problematic: if some processes take longer than others, and if we decide to read |
paulb@40 | 74 | from those processes when they are not ready instead of other processes which |
paulb@40 | 75 | are ready, the whole activity will take much longer than necessary. |
paulb@40 | 76 | |
paulb@40 | 77 | One solution to the problem of knowing when to read from channels is to create |
paulb@40 | 78 | an Exchange object, optionally initialising it with a list of channels through |
paulb@40 | 79 | which data is expected to arrive: |
paulb@40 | 80 | |
paulb@79 | 81 | exchange = pprocess.Exchange() # populate the exchange later |
paulb@79 | 82 | exchange = pprocess.Exchange(channels) # populate the exchange with channels |
paulb@40 | 83 | |
paulb@40 | 84 | We can add channels to the exchange using the add method: |
paulb@40 | 85 | |
paulb@40 | 86 | exchange.add(channel) |
paulb@40 | 87 | |
paulb@40 | 88 | To test whether an exchange is active - that is, whether it is actually |
paulb@40 | 89 | monitoring any channels - we can use the active method which returns all |
paulb@40 | 90 | channels being monitored by the exchange: |
paulb@40 | 91 | |
paulb@40 | 92 | channels = exchange.active() |
paulb@40 | 93 | |
paulb@40 | 94 | We may then check the exchange to see whether any data is ready to be received; |
paulb@40 | 95 | for example: |
paulb@40 | 96 | |
paulb@40 | 97 | for channel in exchange.ready(): |
paulb@40 | 98 | # Read from and write to the channel. |
paulb@40 | 99 | ... |
paulb@40 | 100 | |
paulb@40 | 101 | If we do not wish to wait indefinitely for a list of channels, we can set a |
paulb@40 | 102 | timeout value as an argument to the ready method (as a floating point number |
paulb@40 | 103 | specifying the timeout in seconds, where 0 means a non-blocking poll as stated |
paulb@40 | 104 | in the select module's select function documentation). |
paulb@40 | 105 | |
paulb@67 | 106 | Convenient Message Exchanges |
paulb@67 | 107 | ---------------------------- |
paulb@67 | 108 | |
paulb@67 | 109 | A convenient form of message exchanges can be adopted by defining a subclass of |
paulb@67 | 110 | the Exchange class and defining a particular method: |
paulb@67 | 111 | |
paulb@79 | 112 | class MyExchange(pprocess.Exchange): |
paulb@67 | 113 | def store_data(self, channel): |
paulb@67 | 114 | data = channel.receive() |
paulb@67 | 115 | # Do something with data here. |
paulb@67 | 116 | |
paulb@67 | 117 | The exact operations performed on the received data might be as simple as |
paulb@67 | 118 | storing it on an instance attribute. To make use of the exchange, we would |
paulb@67 | 119 | instantiate it as usual: |
paulb@67 | 120 | |
paulb@67 | 121 | exchange = MyExchange() # populate the exchange later |
paulb@67 | 122 | exchange = MyExchange(limit=10) # set a limit for later population |
paulb@67 | 123 | |
paulb@67 | 124 | The exchange can now be used in a simpler fashion than that shown above. We can |
paulb@67 | 125 | add channels as before using the add method, or we can choose to only add |
paulb@67 | 126 | channels if the specified limit of channels is not exceeded: |
paulb@67 | 127 | |
paulb@67 | 128 | exchange.add(channel) # add a channel as normal |
paulb@67 | 129 | exchange.add_wait(channel) # add a channel, waiting if the limit would be |
paulb@67 | 130 | # exceeded |
paulb@67 | 131 | |
paulb@79 | 132 | We can even start processes and monitor channels without ever handling the |
paulb@79 | 133 | channel ourselves: |
paulb@79 | 134 | |
paulb@79 | 135 | exchange.start(fn, arg1, arg2, named1=value1, named2=value2) |
paulb@79 | 136 | |
paulb@79 | 137 | We can explicitly wait for "free space" for channels by calling the wait method, |
paulb@79 | 138 | although the start and add_wait methods make this less interesting: |
paulb@67 | 139 | |
paulb@67 | 140 | exchange.wait() |
paulb@67 | 141 | |
paulb@67 | 142 | Finally, when finishing the computation, we can choose to merely call the finish |
paulb@67 | 143 | method and have the remaining data processed automatically: |
paulb@67 | 144 | |
paulb@67 | 145 | exchange.finish() |
paulb@67 | 146 | |
paulb@67 | 147 | Clearly, this approach is less flexible but more convenient than the raw message |
paulb@67 | 148 | exchange API as described above. However, it permits much simpler and clearer |
paulb@67 | 149 | code. |
paulb@67 | 150 | |
paulb@40 | 151 | Signals and Waiting |
paulb@40 | 152 | ------------------- |
paulb@40 | 153 | |
paulb@40 | 154 | When created/child processes terminate, one would typically want to be informed |
paulb@40 | 155 | of such conditions using a signal handler. Unfortunately, Python seems to have |
paulb@40 | 156 | issues with restartable reads from file descriptors when interrupted by signals: |
paulb@40 | 157 | |
paulb@40 | 158 | http://mail.python.org/pipermail/python-dev/2002-September/028572.html |
paulb@40 | 159 | http://twistedmatrix.com/bugs/issue733 |
paulb@40 | 160 | |
paulb@40 | 161 | Select and Poll |
paulb@40 | 162 | --------------- |
paulb@40 | 163 | |
paulb@40 | 164 | The exact combination of conditions indicating closed pipes remains relatively |
paulb@40 | 165 | obscure. Here is a message/thread describing them (in the context of another |
paulb@40 | 166 | topic): |
paulb@40 | 167 | |
paulb@40 | 168 | http://twistedmatrix.com/pipermail/twisted-python/2005-February/009666.html |
paulb@40 | 169 | |
paulb@47 | 170 | It would seem, from using sockets and from studying the asyncore module, that |
paulb@40 | 171 | sockets are more predictable than pipes. |
paulb@58 | 172 | |
paulb@58 | 173 | Notes about poll implementations can be found here: |
paulb@58 | 174 | |
paulb@58 | 175 | http://www.greenend.org.uk/rjk/2001/06/poll.html |
paulb@40 | 176 | """ |
paulb@40 | 177 | |
paulb@79 | 178 | __version__ = "0.2.5" |
paulb@40 | 179 | |
paulb@40 | 180 | import os |
paulb@40 | 181 | import sys |
paulb@40 | 182 | import select |
paulb@40 | 183 | import socket |
paulb@40 | 184 | |
paulb@40 | 185 | try: |
paulb@40 | 186 | import cPickle as pickle |
paulb@40 | 187 | except ImportError: |
paulb@40 | 188 | import pickle |
paulb@40 | 189 | |
paulb@40 | 190 | class AcknowledgementError(Exception): |
paulb@40 | 191 | pass |
paulb@40 | 192 | |
paulb@40 | 193 | class Channel: |
paulb@40 | 194 | |
paulb@40 | 195 | "A communications channel." |
paulb@40 | 196 | |
paulb@40 | 197 | def __init__(self, pid, read_pipe, write_pipe): |
paulb@40 | 198 | |
paulb@40 | 199 | """ |
paulb@40 | 200 | Initialise the channel with a process identifier 'pid', a 'read_pipe' |
paulb@40 | 201 | from which messages will be received, and a 'write_pipe' into which |
paulb@40 | 202 | messages will be sent. |
paulb@40 | 203 | """ |
paulb@40 | 204 | |
paulb@40 | 205 | self.pid = pid |
paulb@40 | 206 | self.read_pipe = read_pipe |
paulb@40 | 207 | self.write_pipe = write_pipe |
paulb@40 | 208 | self.closed = 0 |
paulb@40 | 209 | |
paulb@40 | 210 | def __del__(self): |
paulb@40 | 211 | |
paulb@40 | 212 | # Since signals don't work well with I/O, we close pipes and wait for |
paulb@40 | 213 | # created processes upon finalisation. |
paulb@40 | 214 | |
paulb@40 | 215 | self.close() |
paulb@40 | 216 | |
paulb@40 | 217 | def close(self): |
paulb@40 | 218 | |
paulb@40 | 219 | "Explicitly close the channel." |
paulb@40 | 220 | |
paulb@40 | 221 | if not self.closed: |
paulb@40 | 222 | self.closed = 1 |
paulb@40 | 223 | self.read_pipe.close() |
paulb@40 | 224 | self.write_pipe.close() |
paulb@40 | 225 | #self.wait(os.WNOHANG) |
paulb@40 | 226 | |
paulb@40 | 227 | def wait(self, options=0): |
paulb@40 | 228 | |
paulb@40 | 229 | "Wait for the created process, if any, to exit." |
paulb@40 | 230 | |
paulb@40 | 231 | if self.pid != 0: |
paulb@40 | 232 | try: |
paulb@40 | 233 | os.waitpid(self.pid, options) |
paulb@40 | 234 | except OSError: |
paulb@40 | 235 | pass |
paulb@40 | 236 | |
paulb@40 | 237 | def _send(self, obj): |
paulb@40 | 238 | |
paulb@40 | 239 | "Send the given object 'obj' through the channel." |
paulb@40 | 240 | |
paulb@40 | 241 | pickle.dump(obj, self.write_pipe) |
paulb@40 | 242 | self.write_pipe.flush() |
paulb@40 | 243 | |
paulb@40 | 244 | def send(self, obj): |
paulb@40 | 245 | |
paulb@40 | 246 | """ |
paulb@40 | 247 | Send the given object 'obj' through the channel. Then wait for an |
paulb@40 | 248 | acknowledgement. (The acknowledgement makes the caller wait, thus |
paulb@40 | 249 | preventing processes from exiting and disrupting the communications |
paulb@40 | 250 | channel and losing data.) |
paulb@40 | 251 | """ |
paulb@40 | 252 | |
paulb@40 | 253 | self._send(obj) |
paulb@40 | 254 | if self._receive() != "OK": |
paulb@40 | 255 | raise AcknowledgementError, obj |
paulb@40 | 256 | |
paulb@40 | 257 | def _receive(self): |
paulb@40 | 258 | |
paulb@40 | 259 | "Receive an object through the channel, returning the object." |
paulb@40 | 260 | |
paulb@40 | 261 | obj = pickle.load(self.read_pipe) |
paulb@40 | 262 | if isinstance(obj, Exception): |
paulb@40 | 263 | raise obj |
paulb@40 | 264 | else: |
paulb@40 | 265 | return obj |
paulb@40 | 266 | |
paulb@40 | 267 | def receive(self): |
paulb@40 | 268 | |
paulb@40 | 269 | """ |
paulb@40 | 270 | Receive an object through the channel, returning the object. Send an |
paulb@40 | 271 | acknowledgement of receipt. (The acknowledgement makes the sender wait, |
paulb@40 | 272 | thus preventing processes from exiting and disrupting the communications |
paulb@40 | 273 | channel and losing data.) |
paulb@40 | 274 | """ |
paulb@40 | 275 | |
paulb@40 | 276 | try: |
paulb@40 | 277 | obj = self._receive() |
paulb@40 | 278 | return obj |
paulb@40 | 279 | finally: |
paulb@40 | 280 | self._send("OK") |
paulb@40 | 281 | |
paulb@40 | 282 | class Exchange: |
paulb@40 | 283 | |
paulb@40 | 284 | """ |
paulb@40 | 285 | A communications exchange that can be used to detect channels which are |
paulb@67 | 286 | ready to communicate. Subclasses of this class can define the 'store_data' |
paulb@67 | 287 | method in order to enable the 'add_wait', 'wait' and 'finish' methods. |
paulb@40 | 288 | """ |
paulb@40 | 289 | |
paulb@67 | 290 | def __init__(self, channels=None, limit=None, autoclose=1): |
paulb@40 | 291 | |
paulb@40 | 292 | """ |
paulb@67 | 293 | Initialise the exchange with an optional list of 'channels'. |
paulb@67 | 294 | |
paulb@67 | 295 | If the optional 'limit' is specified, restrictions on the addition of |
paulb@67 | 296 | new channels can be enforced and observed through the 'add_wait', 'wait' |
paulb@67 | 297 | and 'finish' methods. To make use of these methods, create a subclass of |
paulb@67 | 298 | this class and define a working 'store_data' method. |
paulb@67 | 299 | |
paulb@67 | 300 | If the optional 'autoclose' parameter is set to a false value, channels |
paulb@67 | 301 | will not be closed automatically when they are removed from the exchange |
paulb@67 | 302 | - by default they are closed when removed. |
paulb@40 | 303 | """ |
paulb@40 | 304 | |
paulb@67 | 305 | self.limit = limit |
paulb@40 | 306 | self.autoclose = autoclose |
paulb@40 | 307 | self.readables = {} |
paulb@58 | 308 | self.removed = [] |
paulb@40 | 309 | self.poller = select.poll() |
paulb@40 | 310 | for channel in channels or []: |
paulb@40 | 311 | self.add(channel) |
paulb@40 | 312 | |
paulb@40 | 313 | def add(self, channel): |
paulb@40 | 314 | |
paulb@40 | 315 | "Add the given 'channel' to the exchange." |
paulb@40 | 316 | |
paulb@40 | 317 | self.readables[channel.read_pipe.fileno()] = channel |
paulb@40 | 318 | self.poller.register(channel.read_pipe.fileno(), select.POLLIN | select.POLLHUP | select.POLLNVAL | select.POLLERR) |
paulb@40 | 319 | |
paulb@40 | 320 | def active(self): |
paulb@40 | 321 | |
paulb@40 | 322 | "Return a list of active channels." |
paulb@40 | 323 | |
paulb@40 | 324 | return self.readables.values() |
paulb@40 | 325 | |
paulb@40 | 326 | def ready(self, timeout=None): |
paulb@40 | 327 | |
paulb@40 | 328 | """ |
paulb@40 | 329 | Wait for a period of time specified by the optional 'timeout' (or until |
paulb@40 | 330 | communication is possible) and return a list of channels which are ready |
paulb@40 | 331 | to be read from. |
paulb@40 | 332 | """ |
paulb@40 | 333 | |
paulb@40 | 334 | fds = self.poller.poll(timeout) |
paulb@40 | 335 | readables = [] |
paulb@58 | 336 | self.removed = [] |
paulb@58 | 337 | |
paulb@40 | 338 | for fd, status in fds: |
paulb@40 | 339 | channel = self.readables[fd] |
paulb@55 | 340 | removed = 0 |
paulb@40 | 341 | |
paulb@40 | 342 | # Remove ended/error channels. |
paulb@40 | 343 | |
paulb@40 | 344 | if status & (select.POLLHUP | select.POLLNVAL | select.POLLERR): |
paulb@40 | 345 | self.remove(channel) |
paulb@58 | 346 | self.removed.append(channel) |
paulb@55 | 347 | removed = 1 |
paulb@40 | 348 | |
paulb@40 | 349 | # Record readable channels. |
paulb@40 | 350 | |
paulb@55 | 351 | if status & select.POLLIN: |
paulb@55 | 352 | if not (removed and self.autoclose): |
paulb@55 | 353 | readables.append(channel) |
paulb@40 | 354 | |
paulb@40 | 355 | return readables |
paulb@40 | 356 | |
paulb@40 | 357 | def remove(self, channel): |
paulb@40 | 358 | |
paulb@40 | 359 | """ |
paulb@40 | 360 | Remove the given 'channel' from the exchange. |
paulb@40 | 361 | """ |
paulb@40 | 362 | |
paulb@40 | 363 | del self.readables[channel.read_pipe.fileno()] |
paulb@40 | 364 | self.poller.unregister(channel.read_pipe.fileno()) |
paulb@40 | 365 | if self.autoclose: |
paulb@40 | 366 | channel.close() |
paulb@40 | 367 | channel.wait() |
paulb@40 | 368 | |
paulb@67 | 369 | # Enhanced exchange methods involving channel limits. |
paulb@67 | 370 | |
paulb@67 | 371 | def add_wait(self, channel): |
paulb@67 | 372 | |
paulb@67 | 373 | """ |
paulb@67 | 374 | Add the given 'channel' to the exchange, waiting if the limit on active |
paulb@67 | 375 | channels would be exceeded by adding the channel. |
paulb@67 | 376 | """ |
paulb@67 | 377 | |
paulb@67 | 378 | self.wait() |
paulb@67 | 379 | self.add(channel) |
paulb@67 | 380 | |
paulb@67 | 381 | def wait(self): |
paulb@67 | 382 | |
paulb@67 | 383 | """ |
paulb@67 | 384 | Test for the limit on channels, blocking and reading incoming data until |
paulb@67 | 385 | the number of channels is below the limit. |
paulb@67 | 386 | """ |
paulb@67 | 387 | |
paulb@67 | 388 | # If limited, block until channels have been closed. |
paulb@67 | 389 | |
paulb@67 | 390 | while self.limit is not None and len(self.active()) >= self.limit: |
paulb@67 | 391 | self.store() |
paulb@67 | 392 | |
paulb@67 | 393 | def finish(self): |
paulb@67 | 394 | |
paulb@67 | 395 | """ |
paulb@67 | 396 | Finish the use of the exchange by waiting for all channels to complete. |
paulb@67 | 397 | """ |
paulb@67 | 398 | |
paulb@67 | 399 | while self.active(): |
paulb@67 | 400 | self.store() |
paulb@67 | 401 | |
paulb@67 | 402 | def store(self): |
paulb@67 | 403 | |
paulb@67 | 404 | "For each ready channel, process the incoming data." |
paulb@67 | 405 | |
paulb@67 | 406 | for channel in self.ready(): |
paulb@67 | 407 | self.store_data(channel) |
paulb@67 | 408 | |
paulb@67 | 409 | def store_data(self, channel): |
paulb@67 | 410 | |
paulb@67 | 411 | """ |
paulb@67 | 412 | Store incoming data from the specified 'channel'. In subclasses of this |
paulb@67 | 413 | class, such data could be stored using instance attributes. |
paulb@67 | 414 | """ |
paulb@67 | 415 | |
paulb@67 | 416 | raise NotImplementedError, "store_data" |
paulb@67 | 417 | |
paulb@79 | 418 | # Convenience methods. |
paulb@79 | 419 | |
paulb@79 | 420 | def start(self, callable, *args, **kwargs): |
paulb@79 | 421 | |
paulb@79 | 422 | """ |
paulb@79 | 423 | Using pprocess.start, create a new process for the given 'callable' |
paulb@79 | 424 | using any additional arguments provided. Then, monitor the channel |
paulb@79 | 425 | created between this process and the created process. |
paulb@79 | 426 | """ |
paulb@79 | 427 | |
paulb@79 | 428 | self.add_wait(start(callable, *args, **kwargs)) |
paulb@79 | 429 | |
paulb@40 | 430 | def create(): |
paulb@40 | 431 | |
paulb@40 | 432 | """ |
paulb@40 | 433 | Create a new process, returning a communications channel to both the |
paulb@40 | 434 | creating process and the created process. |
paulb@40 | 435 | """ |
paulb@40 | 436 | |
paulb@40 | 437 | parent, child = socket.socketpair() |
paulb@40 | 438 | for s in [parent, child]: |
paulb@40 | 439 | s.setblocking(1) |
paulb@40 | 440 | |
paulb@40 | 441 | pid = os.fork() |
paulb@40 | 442 | if pid == 0: |
paulb@40 | 443 | parent.close() |
paulb@73 | 444 | return Channel(pid, child.makefile("r", 0), child.makefile("w", 0)) |
paulb@40 | 445 | else: |
paulb@40 | 446 | child.close() |
paulb@73 | 447 | return Channel(pid, parent.makefile("r", 0), parent.makefile("w", 0)) |
paulb@40 | 448 | |
paulb@40 | 449 | def start(callable, *args, **kwargs): |
paulb@40 | 450 | |
paulb@40 | 451 | """ |
paulb@40 | 452 | Create a new process which shall start running in the given 'callable'. |
paulb@40 | 453 | Return a communications channel to the creating process, and supply such a |
paulb@40 | 454 | channel to the created process as the 'channel' parameter in the given |
paulb@40 | 455 | 'callable'. Additional arguments to the 'callable' can be given as |
paulb@40 | 456 | additional arguments to this function. |
paulb@40 | 457 | """ |
paulb@40 | 458 | |
paulb@40 | 459 | channel = create() |
paulb@40 | 460 | if channel.pid == 0: |
paulb@40 | 461 | try: |
paulb@40 | 462 | try: |
paulb@40 | 463 | callable(channel, *args, **kwargs) |
paulb@40 | 464 | except: |
paulb@40 | 465 | exc_type, exc_value, exc_traceback = sys.exc_info() |
paulb@40 | 466 | channel.send(exc_value) |
paulb@40 | 467 | finally: |
paulb@40 | 468 | channel.close() |
paulb@61 | 469 | os._exit(0) |
paulb@40 | 470 | else: |
paulb@40 | 471 | return channel |
paulb@40 | 472 | |
paulb@40 | 473 | def waitall(): |
paulb@40 | 474 | |
paulb@40 | 475 | "Wait for all created processes to terminate." |
paulb@40 | 476 | |
paulb@40 | 477 | try: |
paulb@40 | 478 | while 1: |
paulb@40 | 479 | os.wait() |
paulb@40 | 480 | except OSError: |
paulb@40 | 481 | pass |
paulb@40 | 482 | |
paulb@40 | 483 | # vim: tabstop=4 expandtab shiftwidth=4 |