| 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>Shared ctypes objects</title> |
| 8 |
<link rel="stylesheet" href="html4css1.css" type="text/css" /> |
| 9 |
</head> |
| 10 |
<body> |
| 11 |
<div class="header"> |
| 12 |
<a class="reference" href="pool-objects.html">Prev</a> <a class="reference" href="processing-ref.html">Up</a> <a class="reference" href="connection-ref.html">Next</a> |
| 13 |
<hr class="header"/> |
| 14 |
</div> |
| 15 |
<div class="document" id="shared-ctypes-objects"> |
| 16 |
<h1 class="title">Shared ctypes objects</h1> |
| 17 |
<p>The <tt class="docutils literal"><span class="pre">processing.sharedctypes</span></tt> module provides functions for allocating |
| 18 |
ctypes objects from shared memory which can be inherited by child |
| 19 |
processes. (See the standard library's documentation for details of |
| 20 |
the <tt class="docutils literal"><span class="pre">ctypes</span></tt> package.)</p> |
| 21 |
<p>The functions in the module are</p> |
| 22 |
<blockquote> |
| 23 |
<dl class="docutils"> |
| 24 |
<dt><tt class="docutils literal"><span class="pre">RawArray(typecode_or_type,</span> <span class="pre">size_or_initializer)</span></tt></dt> |
| 25 |
<dd><p class="first">Returns a ctypes array allocated from shared memory.</p> |
| 26 |
<p><tt class="docutils literal"><span class="pre">typecode_or_type</span></tt> determines the type of the elements of the |
| 27 |
returned array: it is either a ctypes type or a one character |
| 28 |
typecode of the kind used by the <tt class="docutils literal"><span class="pre">array</span></tt> module. If |
| 29 |
<tt class="docutils literal"><span class="pre">size_or_initializer</span></tt> is an integer then it determines the |
| 30 |
length of the array, and the array will be initially zeroed. |
| 31 |
Otherwise <tt class="docutils literal"><span class="pre">size_or_initializer</span></tt> is a sequence which is used to |
| 32 |
initialize the array and whose length determines the length of |
| 33 |
the array.</p> |
| 34 |
<p class="last">Note that setting and getting an element is potentially |
| 35 |
non-atomic -- use Array() instead to make sure that access is |
| 36 |
automatically synchronized using a lock.</p> |
| 37 |
</dd> |
| 38 |
<dt><tt class="docutils literal"><span class="pre">RawValue(typecode_or_type,</span> <span class="pre">*args)</span></tt></dt> |
| 39 |
<dd><p class="first">Returns a ctypes object allocated from shared memory.</p> |
| 40 |
<p><tt class="docutils literal"><span class="pre">typecode_or_type</span></tt> determines the type of the returned object: |
| 41 |
it is either a ctypes type or a one character typecode of the |
| 42 |
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 |
| 43 |
constructor for the type.</p> |
| 44 |
<p>Note that setting and getting the value is potentially |
| 45 |
non-atomic -- use Value() instead to make sure that access is |
| 46 |
automatically synchronized using a lock.</p> |
| 47 |
<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 |
| 48 |
<tt class="docutils literal"><span class="pre">rawvalue</span></tt> attributes which allow one to use it to store and |
| 49 |
retrieve strings -- see documentation for <tt class="docutils literal"><span class="pre">ctypes</span></tt>.</p> |
| 50 |
</dd> |
| 51 |
<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> |
| 52 |
<dd><p class="first">The same as <tt class="docutils literal"><span class="pre">RawArray()</span></tt> except that depending on the value of |
| 53 |
<tt class="docutils literal"><span class="pre">lock</span></tt> a process-safe synchronization wrapper may be returned |
| 54 |
instead of a raw ctypes array.</p> |
| 55 |
<p>If <tt class="docutils literal"><span class="pre">lock</span></tt> is true (the default) then a new lock object is |
| 56 |
created to synchronize access to the value. If <tt class="docutils literal"><span class="pre">lock</span></tt> is a |
| 57 |
<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 |
| 58 |
access to the value. If <tt class="docutils literal"><span class="pre">lock</span></tt> is false then access to the |
| 59 |
returned object will not be automatically protected by a lock, |
| 60 |
so it will not necessarily be "process-safe".</p> |
| 61 |
<p class="last">Note that <tt class="docutils literal"><span class="pre">lock</span></tt> is a keyword only argument.</p> |
| 62 |
</dd> |
| 63 |
<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> |
| 64 |
<dd><p class="first">The same as <tt class="docutils literal"><span class="pre">RawValue()</span></tt> except that depending on the value of |
| 65 |
<tt class="docutils literal"><span class="pre">lock</span></tt> a process-safe synchronization wrapper may be returned |
| 66 |
instead of a raw ctypes object.</p> |
| 67 |
<p>If <tt class="docutils literal"><span class="pre">lock</span></tt> is true (the default) then a new lock object is |
| 68 |
created to synchronize access to the value. If <tt class="docutils literal"><span class="pre">lock</span></tt> is a |
| 69 |
<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 |
| 70 |
access to the value. If <tt class="docutils literal"><span class="pre">lock</span></tt> is false then access to the |
| 71 |
returned object will not be automatically protected by a lock, |
| 72 |
so it will not necessarily be "process-safe".</p> |
| 73 |
<p class="last">Note that <tt class="docutils literal"><span class="pre">lock</span></tt> is a keyword only argument.</p> |
| 74 |
</dd> |
| 75 |
<dt><tt class="docutils literal"><span class="pre">copy(obj)</span></tt></dt> |
| 76 |
<dd>Returns a ctypes object allocated from shared memory which is |
| 77 |
a copy of the ctypes object <tt class="docutils literal"><span class="pre">obj</span></tt>.</dd> |
| 78 |
<dt><tt class="docutils literal"><span class="pre">synchronized(obj,</span> <span class="pre">lock=None)</span></tt></dt> |
| 79 |
<dd><p class="first">Returns a process-safe wrapper object for a ctypes object |
| 80 |
which uses <tt class="docutils literal"><span class="pre">lock</span></tt> to synchronize access. If <tt class="docutils literal"><span class="pre">lock</span></tt> is <tt class="docutils literal"><span class="pre">None</span></tt> |
| 81 |
then a <tt class="docutils literal"><span class="pre">processing.RLock</span></tt> object is created automatically.</p> |
| 82 |
<p>A synchronized wrapper will have two methods in addition to |
| 83 |
those of the object it wraps: <tt class="docutils literal"><span class="pre">getobj()</span></tt> returns the wrapped |
| 84 |
object and <tt class="docutils literal"><span class="pre">getlock()</span></tt> returns the lock object used for |
| 85 |
synchronization.</p> |
| 86 |
<p class="last">Note that accessing the ctypes object through the wrapper can |
| 87 |
be a lot slower than accessing the raw ctypes object.</p> |
| 88 |
</dd> |
| 89 |
</dl> |
| 90 |
</blockquote> |
| 91 |
<div class="section"> |
| 92 |
<h1><a id="equivalences" name="equivalences">Equivalences</a></h1> |
| 93 |
<p>The table below compares the syntax for creating shared ctypes objects |
| 94 |
from shared memory with the normal ctypes syntax. (In the table |
| 95 |
<tt class="docutils literal"><span class="pre">MyStruct</span></tt> is some subclass of <tt class="docutils literal"><span class="pre">ctypes.Structure</span></tt>.)</p> |
| 96 |
<table border="1" class="docutils"> |
| 97 |
<colgroup> |
| 98 |
<col width="27%" /> |
| 99 |
<col width="36%" /> |
| 100 |
<col width="37%" /> |
| 101 |
</colgroup> |
| 102 |
<thead valign="bottom"> |
| 103 |
<tr><th class="head">ctypes</th> |
| 104 |
<th class="head">sharedctypes using type</th> |
| 105 |
<th class="head">sharedctypes using typecode</th> |
| 106 |
</tr> |
| 107 |
</thead> |
| 108 |
<tbody valign="top"> |
| 109 |
<tr><td>c_double(2.4)</td> |
| 110 |
<td>RawValue(c_double, 2.4)</td> |
| 111 |
<td>RawValue('d', 2.4)</td> |
| 112 |
</tr> |
| 113 |
<tr><td>MyStruct(4, 6)</td> |
| 114 |
<td>RawValue(MyStruct, 4, 6)</td> |
| 115 |
<td> </td> |
| 116 |
</tr> |
| 117 |
<tr><td>(c_short * 7)()</td> |
| 118 |
<td>RawArray(c_short, 7)</td> |
| 119 |
<td>RawArray('h', 7)</td> |
| 120 |
</tr> |
| 121 |
<tr><td>(c_int * 3)(9, 2, 8)</td> |
| 122 |
<td>RawArray(c_int, (9, 2, 8))</td> |
| 123 |
<td>RawArray('i', (9, 2, 8))</td> |
| 124 |
</tr> |
| 125 |
</tbody> |
| 126 |
</table> |
| 127 |
</div> |
| 128 |
<div class="section"> |
| 129 |
<h1><a id="example" name="example">Example</a></h1> |
| 130 |
<p>Below is an example where a number of ctypes objects are modified by a |
| 131 |
child process</p> |
| 132 |
<pre class="literal-block"> |
| 133 |
from processing import Process, Lock |
| 134 |
from processing.sharedctypes import Value, Array |
| 135 |
from ctypes import Structure, c_double |
| 136 |
|
| 137 |
class Point(Structure): |
| 138 |
_fields_ = [('x', c_double), ('y', c_double)] |
| 139 |
|
| 140 |
def modify(n, x, s, A): |
| 141 |
n.value **= 2 |
| 142 |
x.value **= 2 |
| 143 |
s.value = s.value.upper() |
| 144 |
for p in A: |
| 145 |
p.x **= 2 |
| 146 |
p.y **= 2 |
| 147 |
|
| 148 |
if __name__ == '__main__': |
| 149 |
lock = Lock() |
| 150 |
|
| 151 |
n = Value('i', 7) |
| 152 |
x = Value(ctypes.c_double, 1.0/3.0, lock=False) |
| 153 |
s = Array('c', 'hello world', lock=lock) |
| 154 |
A = Array(Point, [(1.875,-6.25), (-5.75,2.0), (2.375,9.5)], lock=lock) |
| 155 |
|
| 156 |
p = Process(target=modify, args=(n, x, s, A)) |
| 157 |
p.start() |
| 158 |
p.join() |
| 159 |
|
| 160 |
print n.value |
| 161 |
print x.value |
| 162 |
print s.value |
| 163 |
print [(p.x, p.y) for p in A] |
| 164 |
</pre> |
| 165 |
<p>The results printed are</p> |
| 166 |
<pre class="literal-block"> |
| 167 |
49 |
| 168 |
0.1111111111111111 |
| 169 |
HELLO WORLD |
| 170 |
[(3.515625, 39.0625), (33.0625, 4.0), (5.640625, 90.25)] |
| 171 |
</pre> |
| 172 |
<div class="admonition-avoid-sharing-pointers admonition"> |
| 173 |
<p class="first admonition-title">Avoid sharing pointers</p> |
| 174 |
<p class="last">Although it is posible to store a pointer in shared memory |
| 175 |
remember that this will refer to a location in the address space |
| 176 |
of a specific process. However, the pointer is quite likely to be |
| 177 |
invalid in the context of a second process and trying to |
| 178 |
dereference the pointer from the second process may cause a crash.</p> |
| 179 |
</div> |
| 180 |
</div> |
| 181 |
</div> |
| 182 |
<div class="footer"> |
| 183 |
<hr class="footer" /> |
| 184 |
<a class="reference" href="pool-objects.html">Prev</a> <a class="reference" href="processing-ref.html">Up</a> <a class="reference" href="connection-ref.html">Next</a> |
| 185 |
</div> |
| 186 |
</body> |
| 187 |
</html> |