| 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>processing package reference</title> |
| 8 |
<link rel="stylesheet" href="html4css1.css" type="text/css" /> |
| 9 |
</head> |
| 10 |
<body> |
| 11 |
<div class="header"> |
| 12 |
<a class="reference" href="intro.html">Prev</a> <a class="reference" href="index.html">Up</a> <a class="reference" href="process-objects.html">Next</a> |
| 13 |
<hr class="header"/> |
| 14 |
</div> |
| 15 |
<div class="document" id="processing-package-reference"> |
| 16 |
<h1 class="title">processing package reference</h1> |
| 17 |
<p>The <tt class="docutils literal"><span class="pre">processing</span></tt> package mostly replicates the API of the <tt class="docutils literal"><span class="pre">threading</span></tt> |
| 18 |
module.</p> |
| 19 |
<div class="section"> |
| 20 |
<h1><a id="classes-and-exceptions" name="classes-and-exceptions">Classes and exceptions</a></h1> |
| 21 |
<blockquote> |
| 22 |
<dl class="docutils"> |
| 23 |
<dt><strong>class</strong> <tt class="docutils literal"><span class="pre">Process(group=None,</span> <span class="pre">target=None,</span> <span class="pre">name=None,</span> <span class="pre">args=(),</span> <span class="pre">kwargs={})</span></tt></dt> |
| 24 |
<dd><p class="first">An analogue of <tt class="docutils literal"><span class="pre">threading.Thread</span></tt>.</p> |
| 25 |
<p class="last">See <a class="reference" href="process-objects.html">Process objects</a>.</p> |
| 26 |
</dd> |
| 27 |
<dt><strong>exception</strong> <tt class="docutils literal"><span class="pre">BufferTooShort</span></tt></dt> |
| 28 |
<dd><p class="first">Exception raised by the <tt class="docutils literal"><span class="pre">recvBytesInto()</span></tt> method of a |
| 29 |
<a class="reference" href="connection-objects.html">connection object</a> |
| 30 |
when the supplied buffer object is too small for the message |
| 31 |
read.</p> |
| 32 |
<p class="last">If <tt class="docutils literal"><span class="pre">e</span></tt> is an instance of <tt class="docutils literal"><span class="pre">BufferTooShort</span></tt> then <tt class="docutils literal"><span class="pre">e.args[0]</span></tt> |
| 33 |
will give the message as a byte string.</p> |
| 34 |
</dd> |
| 35 |
</dl> |
| 36 |
</blockquote> |
| 37 |
</div> |
| 38 |
<div class="section"> |
| 39 |
<h1><a id="pipes-and-queues" name="pipes-and-queues">Pipes and Queues</a></h1> |
| 40 |
<p>When using multiple processes one generally uses message passing for |
| 41 |
communication between processes and avoids having to use any |
| 42 |
synchronization primitives like locks.</p> |
| 43 |
<p>For passing messages one can use a pipe (for a connection between two |
| 44 |
processes) or a queue (which allows multiple producers and consumers).</p> |
| 45 |
<p>Note that one can also create a shared queue by using a manager object |
| 46 |
-- see <a class="reference" href="#managers">Managers</a>.</p> |
| 47 |
<p>For an example of the usage of queues for interprocess communication |
| 48 |
see <a class="reference" href="../examples/ex_workers.py">ex_workers.py</a>.</p> |
| 49 |
<blockquote> |
| 50 |
<dl class="docutils"> |
| 51 |
<dt><tt class="docutils literal"><span class="pre">Pipe(duplex=True)</span></tt></dt> |
| 52 |
<dd><p class="first">Returns a pair <tt class="docutils literal"><span class="pre">(conn1,</span> <span class="pre">conn2)</span></tt> of connection objects |
| 53 |
representing the ends of a pipe.</p> |
| 54 |
<p>If <tt class="docutils literal"><span class="pre">duplex</span></tt> is true then the pipe is two way; otherwise |
| 55 |
<tt class="docutils literal"><span class="pre">conn1</span></tt> can only be used for receiving messages and <tt class="docutils literal"><span class="pre">conn2</span></tt> |
| 56 |
can only be used for sending messages.</p> |
| 57 |
<p class="last">See <a class="reference" href="connection-objects.html">Connection objects</a>.</p> |
| 58 |
</dd> |
| 59 |
<dt><tt class="docutils literal"><span class="pre">Queue(maxsize=0)</span></tt></dt> |
| 60 |
<dd><p class="first">Returns a process shared queue object. The usual <tt class="docutils literal"><span class="pre">Empty</span></tt> and |
| 61 |
<tt class="docutils literal"><span class="pre">Full</span></tt> exceptions from the standard library's <tt class="docutils literal"><span class="pre">Queue</span></tt> module |
| 62 |
are raised to signal timeouts.</p> |
| 63 |
<p class="last">See <a class="reference" href="queue-objects.html">Queue objects</a>.</p> |
| 64 |
</dd> |
| 65 |
</dl> |
| 66 |
</blockquote> |
| 67 |
</div> |
| 68 |
<div class="section"> |
| 69 |
<h1><a id="synchronization-primitives" name="synchronization-primitives">Synchronization primitives</a></h1> |
| 70 |
<p>Generally synchronization primitives are not as necessary in a |
| 71 |
multiprocess program as they are in a mulithreaded program. See the |
| 72 |
documentation for the standard library's <tt class="docutils literal"><span class="pre">threading</span></tt> module.</p> |
| 73 |
<p>Note that one can also create synchronization primitves by using a |
| 74 |
manager object -- see <a class="reference" href="#managers">Managers</a>.</p> |
| 75 |
<blockquote> |
| 76 |
<dl class="docutils"> |
| 77 |
<dt><tt class="docutils literal"><span class="pre">BoundedSemaphore(value=1)</span></tt></dt> |
| 78 |
<dd><p class="first">Returns a bounded semaphore object: a clone of |
| 79 |
<tt class="docutils literal"><span class="pre">threading.BoundedSemaphore</span></tt>.</p> |
| 80 |
<p class="last">(On Mac OSX this is indistiguishable from <tt class="docutils literal"><span class="pre">Semaphore()</span></tt> |
| 81 |
because <tt class="docutils literal"><span class="pre">sem_getvalue()</span></tt> is not implemented on that platform).</p> |
| 82 |
</dd> |
| 83 |
<dt><tt class="docutils literal"><span class="pre">Condition(lock=None)</span></tt></dt> |
| 84 |
<dd><p class="first">Returns a condition variable: a clone of <tt class="docutils literal"><span class="pre">threading.Condition</span></tt>.</p> |
| 85 |
<p class="last">If <tt class="docutils literal"><span class="pre">lock</span></tt> is specified then it should be a <tt class="docutils literal"><span class="pre">Lock</span></tt> or <tt class="docutils literal"><span class="pre">RLock</span></tt> |
| 86 |
object from <tt class="docutils literal"><span class="pre">processing</span></tt>.</p> |
| 87 |
</dd> |
| 88 |
<dt><tt class="docutils literal"><span class="pre">Event()</span></tt></dt> |
| 89 |
<dd>Returns an event object: a clone of <tt class="docutils literal"><span class="pre">threading.Event</span></tt>.</dd> |
| 90 |
<dt><tt class="docutils literal"><span class="pre">Lock()</span></tt></dt> |
| 91 |
<dd>Returns a non-recursive lock object: a clone of <tt class="docutils literal"><span class="pre">threading.Lock</span></tt>.</dd> |
| 92 |
<dt><tt class="docutils literal"><span class="pre">RLock()</span></tt></dt> |
| 93 |
<dd>Returns a recursive lock object: a clone of <tt class="docutils literal"><span class="pre">threading.RLock</span></tt>.</dd> |
| 94 |
<dt><tt class="docutils literal"><span class="pre">Semaphore(value=1)</span></tt></dt> |
| 95 |
<dd>Returns a bounded semaphore object: a clone of |
| 96 |
<tt class="docutils literal"><span class="pre">threading.Semaphore</span></tt>.</dd> |
| 97 |
</dl> |
| 98 |
</blockquote> |
| 99 |
<div class="admonition-acquiring-with-a-timeout admonition"> |
| 100 |
<p class="first admonition-title">Acquiring with a timeout</p> |
| 101 |
<p class="last">The <tt class="docutils literal"><span class="pre">acquire()</span></tt> method of <tt class="docutils literal"><span class="pre">BoundedSemaphore</span></tt>, <tt class="docutils literal"><span class="pre">Lock</span></tt>, <tt class="docutils literal"><span class="pre">RLock</span></tt> and |
| 102 |
<tt class="docutils literal"><span class="pre">Semaphore</span></tt> has a timeout parameter not supported by the |
| 103 |
equivalents in <tt class="docutils literal"><span class="pre">threading</span></tt>. The signature is <tt class="docutils literal"><span class="pre">acquire(block=True,</span> |
| 104 |
<span class="pre">timeout=None)</span></tt> with keyword parameters being acceptable. If |
| 105 |
<tt class="docutils literal"><span class="pre">block</span></tt> is true and <tt class="docutils literal"><span class="pre">timeout</span></tt> is not <tt class="docutils literal"><span class="pre">None</span></tt> then it specifies a |
| 106 |
timeout in seconds. If <tt class="docutils literal"><span class="pre">block</span></tt> is false then <tt class="docutils literal"><span class="pre">timeout</span></tt> is ignored.</p> |
| 107 |
</div> |
| 108 |
<div class="admonition-interrupting-the-main-thread admonition"> |
| 109 |
<p class="first admonition-title">Interrupting the main thread</p> |
| 110 |
<p>If the SIGINT signal generated by Ctrl-C arrives while the main |
| 111 |
thread is blocked by a call to <tt class="docutils literal"><span class="pre">BoundedSemaphore.acquire()</span></tt>, |
| 112 |
<tt class="docutils literal"><span class="pre">Lock.acquire()</span></tt>, <tt class="docutils literal"><span class="pre">RLock.acquire()</span></tt>, <tt class="docutils literal"><span class="pre">Semaphore.acquire()</span></tt>, |
| 113 |
<tt class="docutils literal"><span class="pre">Condition.acquire()</span></tt> or <tt class="docutils literal"><span class="pre">Condition.wait()</span></tt> then the call will be |
| 114 |
immediately interrupted and <tt class="docutils literal"><span class="pre">KeyboardInterrupt</span></tt> will be raised.</p> |
| 115 |
<p class="last">This differs from the behaviour of <tt class="docutils literal"><span class="pre">threading</span></tt> where SIGINT will be |
| 116 |
ignored while the equivalent blocking calls are in progress.</p> |
| 117 |
</div> |
| 118 |
</div> |
| 119 |
<div class="section"> |
| 120 |
<h1><a id="shared-objects" name="shared-objects">Shared Objects</a></h1> |
| 121 |
<p>It is possible to create shared objects using shared memory which can |
| 122 |
be inherited by child processes.</p> |
| 123 |
<blockquote> |
| 124 |
<dl class="docutils"> |
| 125 |
<dt><tt class="docutils literal"><span class="pre">Value(typecode_or_type,</span> <span class="pre">*args,</span> <span class="pre">**,</span> <span class="pre">lock=True)</span></tt></dt> |
| 126 |
<dd><p class="first">Returns a ctypes object allocated from shared memory. By |
| 127 |
default the return value is actually a synchronized wrapper |
| 128 |
for the object.</p> |
| 129 |
<p><tt class="docutils literal"><span class="pre">typecode_or_type</span></tt> determines the type of the returned object: |
| 130 |
it is either a ctypes type or a one character typecode of the |
| 131 |
kind used by the <tt class="docutils literal"><span class="pre">array</span></tt> module. <tt class="docutils literal"><span class="pre">*args</span></tt> is passed on to the |
| 132 |
constructor for the type.</p> |
| 133 |
<p>If <tt class="docutils literal"><span class="pre">lock</span></tt> is true (the default) then a new lock object is |
| 134 |
created to synchronize access to the value. If <tt class="docutils literal"><span class="pre">lock</span></tt> is a |
| 135 |
<tt class="docutils literal"><span class="pre">Lock</span></tt> or <tt class="docutils literal"><span class="pre">RLock</span></tt> object then that will be used to synchronize |
| 136 |
access to the value. If <tt class="docutils literal"><span class="pre">lock</span></tt> is false then access to the |
| 137 |
returned object will not be automatically protected by a lock, |
| 138 |
so it will not necessarily be "process-safe".</p> |
| 139 |
<p class="last">Note that <tt class="docutils literal"><span class="pre">lock</span></tt> is a keyword only argument.</p> |
| 140 |
</dd> |
| 141 |
<dt><tt class="docutils literal"><span class="pre">Array(typecode_or_type,</span> <span class="pre">size_or_initializer,</span> <span class="pre">**,</span> <span class="pre">lock=True)</span></tt></dt> |
| 142 |
<dd><p class="first">Returns a ctypes array allocated from shared memory. By |
| 143 |
default the return value is actually a synchronized wrapper |
| 144 |
for the array.</p> |
| 145 |
<p><tt class="docutils literal"><span class="pre">typecode_or_type</span></tt> determines the type of the elements of the |
| 146 |
returned array: it is either a ctypes type or a one character |
| 147 |
typecode of the kind used by the <tt class="docutils literal"><span class="pre">array</span></tt> module. If |
| 148 |
<tt class="docutils literal"><span class="pre">size_or_initializer</span></tt> is an integer then it determines the |
| 149 |
length of the array, and the array will be initially zeroed. |
| 150 |
Otherwise <tt class="docutils literal"><span class="pre">size_or_initializer</span></tt> is a sequence which is used to |
| 151 |
initialize the array and whose length determines the length of |
| 152 |
the array.</p> |
| 153 |
<p>If <tt class="docutils literal"><span class="pre">lock</span></tt> is true (the default) then a new lock object is |
| 154 |
created to synchronize access to the value. If <tt class="docutils literal"><span class="pre">lock</span></tt> is a |
| 155 |
<tt class="docutils literal"><span class="pre">Lock</span></tt> or <tt class="docutils literal"><span class="pre">RLock</span></tt> object then that will be used to synchronize |
| 156 |
access to the value. If <tt class="docutils literal"><span class="pre">lock</span></tt> is false then access to the |
| 157 |
returned object will not be automatically protected by a lock, |
| 158 |
so it will not necessarily be "process-safe".</p> |
| 159 |
<p>Note that <tt class="docutils literal"><span class="pre">lock</span></tt> is a keyword only argument.</p> |
| 160 |
<p class="last">Note that an array of <tt class="docutils literal"><span class="pre">ctypes.c_char</span></tt> has <tt class="docutils literal"><span class="pre">value</span></tt> and |
| 161 |
<tt class="docutils literal"><span class="pre">rawvalue</span></tt> attributes which allow one to use it to store and |
| 162 |
retrieve strings -- see the documentation for ctypes in the |
| 163 |
standard library.</p> |
| 164 |
</dd> |
| 165 |
</dl> |
| 166 |
</blockquote> |
| 167 |
<p>See also <a class="reference" href="sharedctypes.html">sharedctypes</a>.</p> |
| 168 |
</div> |
| 169 |
<div class="section"> |
| 170 |
<h1><a id="managers" name="managers">Managers</a></h1> |
| 171 |
<p>Managers provide a way to create data which can be shared between |
| 172 |
different processes.</p> |
| 173 |
<blockquote> |
| 174 |
<dl class="docutils"> |
| 175 |
<dt><tt class="docutils literal"><span class="pre">Manager()</span></tt></dt> |
| 176 |
<dd><p class="first">Returns a started <tt class="docutils literal"><span class="pre">SyncManager</span></tt> object which can be |
| 177 |
used for sharing objects between processes. The returned |
| 178 |
manager object corresponds to a spawned child process and has |
| 179 |
methods which will create shared objects and return |
| 180 |
corresponding proxies.</p> |
| 181 |
<p>The methods for creating shared objects are</p> |
| 182 |
<blockquote> |
| 183 |
<tt class="docutils literal"><span class="pre">list()</span></tt>, <tt class="docutils literal"><span class="pre">dict()</span></tt>, <tt class="docutils literal"><span class="pre">Namespace()</span></tt>, <tt class="docutils literal"><span class="pre">Value()</span></tt>, |
| 184 |
<tt class="docutils literal"><span class="pre">Array()</span></tt>, <tt class="docutils literal"><span class="pre">Lock()</span></tt>, <tt class="docutils literal"><span class="pre">RLock()</span></tt>, <tt class="docutils literal"><span class="pre">Semaphore()</span></tt>, |
| 185 |
<tt class="docutils literal"><span class="pre">BoundedSemaphore()</span></tt>, <tt class="docutils literal"><span class="pre">Condition()</span></tt>, <tt class="docutils literal"><span class="pre">Event()</span></tt>, <tt class="docutils literal"><span class="pre">Queue()</span></tt>.</blockquote> |
| 186 |
<p class="last">See <a class="reference" href="manager-objects.html#sync-manager">SyncManager</a>.</p> |
| 187 |
</dd> |
| 188 |
</dl> |
| 189 |
</blockquote> |
| 190 |
<p>It is possible to create managers which support other types -- see |
| 191 |
<a class="reference" href="manager-objects.html#customized-managers">Customized managers</a>.</p> |
| 192 |
</div> |
| 193 |
<div class="section"> |
| 194 |
<h1><a id="process-pools" name="process-pools">Process Pools</a></h1> |
| 195 |
<p>One can create a pool of processes which will carry out tasks |
| 196 |
submitted to it.</p> |
| 197 |
<blockquote> |
| 198 |
<dl class="docutils"> |
| 199 |
<dt><tt class="docutils literal"><span class="pre">Pool(processes=None,</span> <span class="pre">initializer=None,</span> <span class="pre">initargs=())</span></tt></dt> |
| 200 |
<dd><p class="first">Returns a process pool object which controls a pool of worker |
| 201 |
processes to which jobs can be submitted.</p> |
| 202 |
<p>It supports asynchronous results with timeouts and |
| 203 |
callbacks and has a parallel map implementation.</p> |
| 204 |
<p><tt class="docutils literal"><span class="pre">processes</span></tt> is the number of worker processes to use. If |
| 205 |
<tt class="docutils literal"><span class="pre">processes</span></tt> is <tt class="docutils literal"><span class="pre">None</span></tt> then the number returned by <tt class="docutils literal"><span class="pre">cpuCount()</span></tt> |
| 206 |
is used. If <tt class="docutils literal"><span class="pre">initializer</span></tt> is not <tt class="docutils literal"><span class="pre">None</span></tt> then each worker |
| 207 |
process will call <tt class="docutils literal"><span class="pre">initializer(*initargs)</span></tt> when it starts.</p> |
| 208 |
<p class="last">See <a class="reference" href="pool-objects.html">Pool objects</a>.</p> |
| 209 |
</dd> |
| 210 |
</dl> |
| 211 |
</blockquote> |
| 212 |
</div> |
| 213 |
<div class="section"> |
| 214 |
<h1><a id="logging" name="logging">Logging</a></h1> |
| 215 |
<p>Some support for logging is available. Note, however, that the |
| 216 |
<tt class="docutils literal"><span class="pre">logging</span></tt> package does not use process shared locks so it is possible |
| 217 |
(depending on the handler type) for messages from different processes |
| 218 |
to get mixed up.</p> |
| 219 |
<blockquote> |
| 220 |
<dl class="docutils"> |
| 221 |
<dt><tt class="docutils literal"><span class="pre">enableLogging(level,</span> <span class="pre">HandlerType=None,</span> <span class="pre">handlerArgs=(),</span> <span class="pre">format=None)</span></tt></dt> |
| 222 |
<dd><p class="first">Enables logging and sets the debug level used by the package's |
| 223 |
logger to <tt class="docutils literal"><span class="pre">level</span></tt>. See documentation for the <tt class="docutils literal"><span class="pre">logging</span></tt> module |
| 224 |
in the standard library.</p> |
| 225 |
<p>If <tt class="docutils literal"><span class="pre">HandlerType</span></tt> is specified then a handler is created using |
| 226 |
<tt class="docutils literal"><span class="pre">HandlerType(*handlerArgs)</span></tt> and this will be used by the |
| 227 |
logger -- any previous handlers will be discarded. If |
| 228 |
<tt class="docutils literal"><span class="pre">format</span></tt> is specified then this will be used for the handler; |
| 229 |
otherwise <tt class="docutils literal"><span class="pre">format</span></tt> defaults to |
| 230 |
<tt class="docutils literal"><span class="pre">'[%(levelname)s/%(processName)s]</span> <span class="pre">%(message)s'</span></tt>. (The logger |
| 231 |
used by <tt class="docutils literal"><span class="pre">processing</span></tt> allows use of the non-standard |
| 232 |
<tt class="docutils literal"><span class="pre">'%(processName)s'</span></tt> format.)</p> |
| 233 |
<p>If <tt class="docutils literal"><span class="pre">HandlerType</span></tt> is not specified and the logger has no |
| 234 |
handlers then a default one is created which prints to |
| 235 |
<tt class="docutils literal"><span class="pre">sys.stderr</span></tt>.</p> |
| 236 |
<p class="last"><em>Note</em>: on Windows a child process does not directly inherit |
| 237 |
its parent's logger; instead it will automatically call |
| 238 |
<tt class="docutils literal"><span class="pre">enableLogging()</span></tt> with the same arguments which were used when |
| 239 |
its parent process last called <tt class="docutils literal"><span class="pre">enableLogging()</span></tt> (if it ever |
| 240 |
did).</p> |
| 241 |
</dd> |
| 242 |
<dt><tt class="docutils literal"><span class="pre">getLogger()</span></tt></dt> |
| 243 |
<dd>Returns the logger used by <tt class="docutils literal"><span class="pre">processing</span></tt>. If <tt class="docutils literal"><span class="pre">enableLogging()</span></tt> |
| 244 |
has not yet been called then <tt class="docutils literal"><span class="pre">None</span></tt> is returned.</dd> |
| 245 |
</dl> |
| 246 |
</blockquote> |
| 247 |
<p>Below is an example session with logging turned on:</p> |
| 248 |
<pre class="literal-block"> |
| 249 |
>>> import processing, logging |
| 250 |
>>> processing.enableLogging(level=logging.INFO) |
| 251 |
>>> processing.getLogger().warning('doomed') |
| 252 |
[WARNING/MainProcess] doomed |
| 253 |
>>> m = processing.Manager() |
| 254 |
[INFO/SyncManager-1] child process calling self.run() |
| 255 |
[INFO/SyncManager-1] manager bound to '\\\\.\\pipe\\pyc-2776-0-lj0tfa' |
| 256 |
>>> del m |
| 257 |
[INFO/MainProcess] sending shutdown message to manager |
| 258 |
[INFO/SyncManager-1] manager received shutdown message |
| 259 |
[INFO/SyncManager-1] manager exiting with exitcode 0 |
| 260 |
</pre> |
| 261 |
</div> |
| 262 |
<div class="section"> |
| 263 |
<h1><a id="miscellaneous" name="miscellaneous">Miscellaneous</a></h1> |
| 264 |
<blockquote> |
| 265 |
<dl class="docutils"> |
| 266 |
<dt><tt class="docutils literal"><span class="pre">activeChildren()</span></tt></dt> |
| 267 |
<dd><p class="first">Return list of all live children of the current process.</p> |
| 268 |
<p class="last">Calling this has the side affect of "joining" any processes |
| 269 |
which have already finished.</p> |
| 270 |
</dd> |
| 271 |
<dt><tt class="docutils literal"><span class="pre">cpuCount()</span></tt></dt> |
| 272 |
<dd>Returns the number of CPUs in the system. May raise |
| 273 |
<tt class="docutils literal"><span class="pre">NotImplementedError</span></tt>.</dd> |
| 274 |
<dt><tt class="docutils literal"><span class="pre">currentProcess()</span></tt></dt> |
| 275 |
<dd><p class="first">An analogue of <tt class="docutils literal"><span class="pre">threading.current_thread()</span></tt>.</p> |
| 276 |
<p class="last">Returns the object corresponding to the current process.</p> |
| 277 |
</dd> |
| 278 |
<dt><tt class="docutils literal"><span class="pre">freezeSupport()</span></tt></dt> |
| 279 |
<dd><p class="first">Adds support for when a program which uses the <tt class="docutils literal"><span class="pre">processing</span></tt> |
| 280 |
package has been frozen to produce a Windows executable. (Has |
| 281 |
been tested with <tt class="docutils literal"><span class="pre">py2exe</span></tt>, <tt class="docutils literal"><span class="pre">PyInstaller</span></tt> and <tt class="docutils literal"><span class="pre">cx_Freeze</span></tt>.)</p> |
| 282 |
<p>One needs to call this function straight after the <tt class="docutils literal"><span class="pre">if</span> <span class="pre">__name__</span> |
| 283 |
<span class="pre">==</span> <span class="pre">'__main__'</span></tt> line of the main module. For example</p> |
| 284 |
<pre class="literal-block"> |
| 285 |
from processing import Process, freezeSupport |
| 286 |
|
| 287 |
def f(): |
| 288 |
print 'hello world!' |
| 289 |
|
| 290 |
if __name__ == '__main__': |
| 291 |
freezeSupport() |
| 292 |
Process(target=f).start() |
| 293 |
</pre> |
| 294 |
<p>If the <tt class="docutils literal"><span class="pre">freezeSupport()</span></tt> line is missed out then trying to run |
| 295 |
the frozen executable will raise <tt class="docutils literal"><span class="pre">RuntimeError</span></tt>.</p> |
| 296 |
<p class="last">If the module is being run normally by the python interpreter |
| 297 |
then <tt class="docutils literal"><span class="pre">freezeSupport()</span></tt> has no effect.</p> |
| 298 |
</dd> |
| 299 |
</dl> |
| 300 |
</blockquote> |
| 301 |
<div class="note"> |
| 302 |
<p class="first admonition-title">Note</p> |
| 303 |
<ul class="last simple"> |
| 304 |
<li>The <tt class="docutils literal"><span class="pre">processing.dummy</span></tt> package replicates the API of <tt class="docutils literal"><span class="pre">processing</span></tt> |
| 305 |
but is no more than a wrapper around the <tt class="docutils literal"><span class="pre">threading</span></tt> module.</li> |
| 306 |
<li><tt class="docutils literal"><span class="pre">processing</span></tt> contains no analogues of <tt class="docutils literal"><span class="pre">activeCount</span></tt>, |
| 307 |
<tt class="docutils literal"><span class="pre">enumerate</span></tt>, <tt class="docutils literal"><span class="pre">settrace</span></tt>, <tt class="docutils literal"><span class="pre">setprofile</span></tt>, <tt class="docutils literal"><span class="pre">Timer</span></tt>, or |
| 308 |
<tt class="docutils literal"><span class="pre">local</span></tt> from the <tt class="docutils literal"><span class="pre">threading</span></tt> module.</li> |
| 309 |
</ul> |
| 310 |
</div> |
| 311 |
</div> |
| 312 |
<div class="section"> |
| 313 |
<h1><a id="subsections" name="subsections">Subsections</a></h1> |
| 314 |
<ul class="simple"> |
| 315 |
<li><a class="reference" href="process-objects.html">Process objects</a></li> |
| 316 |
<li><a class="reference" href="queue-objects.html">Queue objects</a></li> |
| 317 |
<li><a class="reference" href="connection-objects.html">Connection objects</a></li> |
| 318 |
<li><a class="reference" href="manager-objects.html">Manager objects</a></li> |
| 319 |
<li><a class="reference" href="proxy-objects.html">Proxy objects</a></li> |
| 320 |
<li><a class="reference" href="pool-objects.html">Pool objects</a></li> |
| 321 |
<li><a class="reference" href="sharedctypes.html">Shared ctypes object</a></li> |
| 322 |
<li><a class="reference" href="connection-ref.html">Listeners and Clients</a></li> |
| 323 |
</ul> |
| 324 |
</div> |
| 325 |
</div> |
| 326 |
<div class="footer"> |
| 327 |
<hr class="footer" /> |
| 328 |
<a class="reference" href="intro.html">Prev</a> <a class="reference" href="index.html">Up</a> <a class="reference" href="process-objects.html">Next</a> |
| 329 |
</div> |
| 330 |
</body> |
| 331 |
</html> |