| 1 |
<?xml version="1.0" encoding="utf-8" ?> |
| 2 |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
| 3 |
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> |
| 4 |
<head> |
| 5 |
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> |
| 6 |
<meta name="generator" content="Docutils 0.4: http://docutils.sourceforge.net/" /> |
| 7 |
<title>Python processing</title> |
| 8 |
<meta name="author" content="R Oudkerk" /> |
| 9 |
<link rel="stylesheet" href="doc/html4css1.css" type="text/css" /> |
| 10 |
</head> |
| 11 |
<body> |
| 12 |
<div class="document" id="python-processing"> |
| 13 |
<h1 class="title">Python processing</h1> |
| 14 |
<table class="docinfo" frame="void" rules="none"> |
| 15 |
<col class="docinfo-name" /> |
| 16 |
<col class="docinfo-content" /> |
| 17 |
<tbody valign="top"> |
| 18 |
<tr><th class="docinfo-name">Author:</th> |
| 19 |
<td>R Oudkerk</td></tr> |
| 20 |
<tr><th class="docinfo-name">Contact:</th> |
| 21 |
<td>roudkerk at users.berlios.de</td></tr> |
| 22 |
<tr class="field"><th class="docinfo-name">Url:</th><td class="field-body"><a class="reference" href="http://developer.berlios.de/projects/pyprocessing">http://developer.berlios.de/projects/pyprocessing</a></td> |
| 23 |
</tr> |
| 24 |
<tr><th class="docinfo-name">Version:</th> |
| 25 |
<td>0.52</td></tr> |
| 26 |
<tr class="field"><th class="docinfo-name">Licence:</th><td class="field-body">BSD Licence</td> |
| 27 |
</tr> |
| 28 |
</tbody> |
| 29 |
</table> |
| 30 |
<p><tt class="docutils literal"><span class="pre">processing</span></tt> is a package for the Python language which supports the |
| 31 |
spawning of processes using the API of the standard library's |
| 32 |
<tt class="docutils literal"><span class="pre">threading</span></tt> module. It runs on both Unix and Windows.</p> |
| 33 |
<p>Features:</p> |
| 34 |
<ul class="simple"> |
| 35 |
<li>Objects can be transferred between processes using pipes or |
| 36 |
multi-producer/multi-consumer queues.</li> |
| 37 |
<li>Objects can be shared between processes using a server process or |
| 38 |
(for simple data) shared memory.</li> |
| 39 |
<li>Equivalents of all the synchronization primitives in <tt class="docutils literal"><span class="pre">threading</span></tt> |
| 40 |
are available.</li> |
| 41 |
<li>A <tt class="docutils literal"><span class="pre">Pool</span></tt> class makes it easy to submit tasks to a pool of worker |
| 42 |
processes.</li> |
| 43 |
</ul> |
| 44 |
<div class="section"> |
| 45 |
<h1><a id="links" name="links">Links</a></h1> |
| 46 |
<ul class="simple"> |
| 47 |
<li><a class="reference" href="./doc/index.html">Documentation</a></li> |
| 48 |
<li><a class="reference" href="./doc/INSTALL.html">Installation instructions</a></li> |
| 49 |
<li><a class="reference" href="./doc/CHANGES.html">Changelog</a></li> |
| 50 |
<li><a class="reference" href="./doc/THANKS.html">Acknowledgments</a></li> |
| 51 |
<li><a class="reference" href="./doc/COPYING.html">BSD Licence</a></li> |
| 52 |
</ul> |
| 53 |
<p>The project is hosted at</p> |
| 54 |
<ul class="simple"> |
| 55 |
<li><a class="reference" href="http://developer.berlios.de/projects/pyprocessing">http://developer.berlios.de/projects/pyprocessing</a></li> |
| 56 |
</ul> |
| 57 |
<p>The package can be downloaded from</p> |
| 58 |
<ul class="simple"> |
| 59 |
<li><a class="reference" href="http://developer.berlios.de/project/filelist.php?group_id=9001">http://developer.berlios.de/project/filelist.php?group_id=9001</a> or</li> |
| 60 |
<li><a class="reference" href="http://pypi.python.org/pypi/processing">http://pypi.python.org/pypi/processing</a></li> |
| 61 |
</ul> |
| 62 |
</div> |
| 63 |
<div class="section"> |
| 64 |
<h1><a id="examples" name="examples">Examples</a></h1> |
| 65 |
<p>The <tt class="docutils literal"><span class="pre">processing.Process</span></tt> class follows the API of <tt class="docutils literal"><span class="pre">threading.Thread</span></tt>. |
| 66 |
For example</p> |
| 67 |
<pre class="literal-block"> |
| 68 |
from processing import Process, Queue |
| 69 |
|
| 70 |
def f(q): |
| 71 |
q.put('hello world') |
| 72 |
|
| 73 |
if __name__ == '__main__': |
| 74 |
q = Queue() |
| 75 |
p = Process(target=f, args=[q]) |
| 76 |
p.start() |
| 77 |
print q.get() |
| 78 |
p.join() |
| 79 |
</pre> |
| 80 |
<p>Synchronization primitives like locks, semaphores and conditions are |
| 81 |
available, for example</p> |
| 82 |
<pre class="literal-block"> |
| 83 |
>>> from processing import Condition |
| 84 |
>>> c = Condition() |
| 85 |
>>> print c |
| 86 |
<Condition(<RLock(None, 0)>), 0> |
| 87 |
>>> c.acquire() |
| 88 |
True |
| 89 |
>>> print c |
| 90 |
<Condition(<RLock(MainProcess, 1)>), 0> |
| 91 |
</pre> |
| 92 |
<p>One can also use a manager to create shared objects either in shared |
| 93 |
memory or in a server process, for example</p> |
| 94 |
<pre class="literal-block"> |
| 95 |
>>> from processing import Manager |
| 96 |
>>> manager = Manager() |
| 97 |
>>> l = manager.list(range(10)) |
| 98 |
>>> l.reverse() |
| 99 |
>>> print l |
| 100 |
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0] |
| 101 |
>>> print repr(l) |
| 102 |
<Proxy[list] object at 0x00E1B3B0> |
| 103 |
</pre> |
| 104 |
<p>Tasks can be offloaded to a pool of worker processes in various ways, |
| 105 |
for example</p> |
| 106 |
<pre class="literal-block"> |
| 107 |
>>> from processing import Pool |
| 108 |
>>> def f(x): return x*x |
| 109 |
... |
| 110 |
>>> p = Pool(4) |
| 111 |
>>> result = p.mapAsync(f, range(10)) |
| 112 |
>>> print result.get(timeout=1) |
| 113 |
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81] |
| 114 |
</pre> |
| 115 |
<a href="http://developer.berlios.de" title="BerliOS Developer"> |
| 116 |
<img src="http://developer.berlios.de/bslogo.php?group_id=9001" |
| 117 |
width="124px" height="32px" border="0" alt="BerliOS Developer Logo"></a></div> |
| 118 |
</div> |
| 119 |
</body> |
| 120 |
</html> |