Commit | Line | Data |
---|---|---|
86530b38 AT |
1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
2 | <html> | |
3 | <head> | |
4 | <link rel="STYLESHEET" href="lib.css" type='text/css' /> | |
5 | <link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /> | |
6 | <link rel='start' href='../index.html' title='Python Documentation Index' /> | |
7 | <link rel="first" href="lib.html" title='Python Library Reference' /> | |
8 | <link rel='contents' href='contents.html' title="Contents" /> | |
9 | <link rel='index' href='genindex.html' title='Index' /> | |
10 | <link rel='last' href='about.html' title='About this document...' /> | |
11 | <link rel='help' href='about.html' title='About this document...' /> | |
12 | <link rel="next" href="event-objects.html" /> | |
13 | <link rel="prev" href="condition-objects.html" /> | |
14 | <link rel="parent" href="module-threading.html" /> | |
15 | <link rel="next" href="semaphore-examples.html" /> | |
16 | <meta name='aesop' content='information' /> | |
17 | <title>7.5.4 Semaphore Objects </title> | |
18 | </head> | |
19 | <body> | |
20 | <DIV CLASS="navigation"> | |
21 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> | |
22 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
23 | <tr> | |
24 | <td class='online-navigation'><a rel="prev" title="7.5.3 Condition Objects" | |
25 | href="condition-objects.html"><img src='../icons/previous.png' | |
26 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
27 | <td class='online-navigation'><a rel="parent" title="7.5 threading " | |
28 | href="module-threading.html"><img src='../icons/up.png' | |
29 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
30 | <td class='online-navigation'><a rel="next" title="7.5.4.1 Semaphore Example" | |
31 | href="semaphore-examples.html"><img src='../icons/next.png' | |
32 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
33 | <td align="center" width="100%">Python Library Reference</td> | |
34 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
35 | href="contents.html"><img src='../icons/contents.png' | |
36 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
37 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' | |
38 | border='0' height='32' alt='Module Index' width='32' /></a></td> | |
39 | <td class='online-navigation'><a rel="index" title="Index" | |
40 | href="genindex.html"><img src='../icons/index.png' | |
41 | border='0' height='32' alt='Index' width='32' /></A></td> | |
42 | </tr></table> | |
43 | <div class='online-navigation'> | |
44 | <b class="navlabel">Previous:</b> | |
45 | <a class="sectref" rel="prev" href="condition-objects.html">7.5.3 Condition Objects</A> | |
46 | <b class="navlabel">Up:</b> | |
47 | <a class="sectref" rel="parent" href="module-threading.html">7.5 threading </A> | |
48 | <b class="navlabel">Next:</b> | |
49 | <a class="sectref" rel="next" href="semaphore-examples.html">7.5.4.1 Semaphore Example</A> | |
50 | </div> | |
51 | <hr /></div> | |
52 | </DIV> | |
53 | <!--End of Navigation Panel--> | |
54 | ||
55 | <H2><A NAME="SECTION009540000000000000000"></A><A NAME="semaphore-objects"></A> | |
56 | <BR> | |
57 | 7.5.4 Semaphore Objects | |
58 | </H2> | |
59 | ||
60 | <P> | |
61 | This is one of the oldest synchronization primitives in the history of | |
62 | computer science, invented by the early Dutch computer scientist | |
63 | Edsger W. Dijkstra (he used <tt class="method">P()</tt> and <tt class="method">V()</tt> instead of | |
64 | <tt class="method">acquire()</tt> and <tt class="method">release()</tt>). | |
65 | ||
66 | <P> | |
67 | A semaphore manages an internal counter which is decremented by each | |
68 | <tt class="method">acquire()</tt> call and incremented by each <tt class="method">release()</tt> | |
69 | call. The counter can never go below zero; when <tt class="method">acquire()</tt> | |
70 | finds that it is zero, it blocks, waiting until some other thread | |
71 | calls <tt class="method">release()</tt>. | |
72 | ||
73 | <P> | |
74 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
75 | <td><nobr><b><span class="typelabel">class</span> <tt id='l2h-2730' xml:id='l2h-2730' class="class">Semaphore</tt></b>(</nobr></td> | |
76 | <td><var></var><big>[</big><var>value</var><big>]</big><var></var>)</td></tr></table></dt> | |
77 | <dd> | |
78 | The optional argument gives the initial value for the internal | |
79 | counter; it defaults to <code>1</code>. | |
80 | </dl> | |
81 | ||
82 | <P> | |
83 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
84 | <td><nobr><b><tt id='l2h-2731' xml:id='l2h-2731' class="method">acquire</tt></b>(</nobr></td> | |
85 | <td><var></var><big>[</big><var>blocking</var><big>]</big><var></var>)</td></tr></table></dt> | |
86 | <dd> | |
87 | Acquire a semaphore. | |
88 | ||
89 | <P> | |
90 | When invoked without arguments: if the internal counter is larger than | |
91 | zero on entry, decrement it by one and return immediately. If it is | |
92 | zero on entry, block, waiting until some other thread has called | |
93 | <tt class="method">release()</tt> to make it larger than zero. This is done with | |
94 | proper interlocking so that if multiple <tt class="method">acquire()</tt> calls are | |
95 | blocked, <tt class="method">release()</tt> will wake exactly one of them up. The | |
96 | implementation may pick one at random, so the order in which blocked | |
97 | threads are awakened should not be relied on. There is no return | |
98 | value in this case. | |
99 | ||
100 | <P> | |
101 | When invoked with <var>blocking</var> set to true, do the same thing as | |
102 | when called without arguments, and return true. | |
103 | ||
104 | <P> | |
105 | When invoked with <var>blocking</var> set to false, do not block. If a | |
106 | call without an argument would block, return false immediately; | |
107 | otherwise, do the same thing as when called without arguments, and | |
108 | return true. | |
109 | </dl> | |
110 | ||
111 | <P> | |
112 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
113 | <td><nobr><b><tt id='l2h-2732' xml:id='l2h-2732' class="method">release</tt></b>(</nobr></td> | |
114 | <td><var></var>)</td></tr></table></dt> | |
115 | <dd> | |
116 | Release a semaphore, | |
117 | incrementing the internal counter by one. When it was zero on | |
118 | entry and another thread is waiting for it to become larger | |
119 | than zero again, wake up that thread. | |
120 | </dl> | |
121 | ||
122 | <P> | |
123 | ||
124 | <p><br /></p><hr class='online-navigation' /> | |
125 | <div class='online-navigation'> | |
126 | <!--Table of Child-Links--> | |
127 | <A NAME="CHILD_LINKS"><STRONG>Subsections</STRONG></a> | |
128 | ||
129 | <UL CLASS="ChildLinks"> | |
130 | <LI><A href="semaphore-examples.html">7.5.4.1 <tt class="class">Semaphore</tt> Example</a> | |
131 | </ul> | |
132 | <!--End of Table of Child-Links--> | |
133 | </div> | |
134 | ||
135 | <DIV CLASS="navigation"> | |
136 | <div class='online-navigation'> | |
137 | <p></p><hr /> | |
138 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
139 | <tr> | |
140 | <td class='online-navigation'><a rel="prev" title="7.5.3 Condition Objects" | |
141 | href="condition-objects.html"><img src='../icons/previous.png' | |
142 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
143 | <td class='online-navigation'><a rel="parent" title="7.5 threading " | |
144 | href="module-threading.html"><img src='../icons/up.png' | |
145 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
146 | <td class='online-navigation'><a rel="next" title="7.5.4.1 Semaphore Example" | |
147 | href="semaphore-examples.html"><img src='../icons/next.png' | |
148 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
149 | <td align="center" width="100%">Python Library Reference</td> | |
150 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
151 | href="contents.html"><img src='../icons/contents.png' | |
152 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
153 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' | |
154 | border='0' height='32' alt='Module Index' width='32' /></a></td> | |
155 | <td class='online-navigation'><a rel="index" title="Index" | |
156 | href="genindex.html"><img src='../icons/index.png' | |
157 | border='0' height='32' alt='Index' width='32' /></A></td> | |
158 | </tr></table> | |
159 | <div class='online-navigation'> | |
160 | <b class="navlabel">Previous:</b> | |
161 | <a class="sectref" rel="prev" href="condition-objects.html">7.5.3 Condition Objects</A> | |
162 | <b class="navlabel">Up:</b> | |
163 | <a class="sectref" rel="parent" href="module-threading.html">7.5 threading </A> | |
164 | <b class="navlabel">Next:</b> | |
165 | <a class="sectref" rel="next" href="semaphore-examples.html">7.5.4.1 Semaphore Example</A> | |
166 | </div> | |
167 | </div> | |
168 | <hr /> | |
169 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> | |
170 | </DIV> | |
171 | <!--End of Navigation Panel--> | |
172 | <ADDRESS> | |
173 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. | |
174 | </ADDRESS> | |
175 | </BODY> | |
176 | </HTML> |