Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 1 | .. _sync: |
| 2 | |
| 3 | Synchronization primitives |
| 4 | ========================== |
| 5 | |
| 6 | Locks |
| 7 | ----- |
| 8 | |
| 9 | .. class:: Lock(\*, loop=None) |
| 10 | |
| 11 | Primitive lock objects. |
| 12 | |
| 13 | A primitive lock is a synchronization primitive that is not owned by a |
| 14 | particular coroutine when locked. A primitive lock is in one of two states, |
| 15 | 'locked' or 'unlocked'. |
| 16 | |
| 17 | It is created in the unlocked state. It has two basic methods, :meth:`acquire` |
| 18 | and :meth:`release`. When the state is unlocked, acquire() changes the state to |
| 19 | locked and returns immediately. When the state is locked, acquire() blocks |
| 20 | until a call to release() in another coroutine changes it to unlocked, then |
| 21 | the acquire() call resets it to locked and returns. The release() method |
| 22 | should only be called in the locked state; it changes the state to unlocked |
| 23 | and returns immediately. If an attempt is made to release an unlocked lock, |
| 24 | a :exc:`RuntimeError` will be raised. |
| 25 | |
| 26 | When more than one coroutine is blocked in acquire() waiting for the state |
| 27 | to turn to unlocked, only one coroutine proceeds when a release() call |
| 28 | resets the state to unlocked; first coroutine which is blocked in acquire() |
| 29 | is being processed. |
| 30 | |
| 31 | :meth:`acquire` is a coroutine and should be called with ``yield from``. |
| 32 | |
| 33 | Locks also support the context manager protocol. ``(yield from lock)`` |
| 34 | should be used as context manager expression. |
| 35 | |
| 36 | Usage:: |
| 37 | |
| 38 | lock = Lock() |
| 39 | ... |
| 40 | yield from lock |
| 41 | try: |
| 42 | ... |
| 43 | finally: |
| 44 | lock.release() |
| 45 | |
| 46 | Context manager usage:: |
| 47 | |
| 48 | lock = Lock() |
| 49 | ... |
| 50 | with (yield from lock): |
| 51 | ... |
| 52 | |
| 53 | Lock objects can be tested for locking state:: |
| 54 | |
| 55 | if not lock.locked(): |
| 56 | yield from lock |
| 57 | else: |
| 58 | # lock is acquired |
| 59 | ... |
| 60 | |
| 61 | .. method:: locked() |
| 62 | |
| 63 | Return ``True`` if lock is acquired. |
| 64 | |
| 65 | .. method:: acquire() |
| 66 | |
| 67 | Acquire a lock. |
| 68 | |
| 69 | This method blocks until the lock is unlocked, then sets it to locked and |
| 70 | returns ``True``. |
| 71 | |
| 72 | This method returns a :ref:`coroutine <coroutine>`. |
| 73 | |
| 74 | .. method:: release() |
| 75 | |
| 76 | Release a lock. |
| 77 | |
| 78 | When the lock is locked, reset it to unlocked, and return. If any other |
| 79 | coroutines are blocked waiting for the lock to become unlocked, allow |
| 80 | exactly one of them to proceed. |
| 81 | |
| 82 | When invoked on an unlocked lock, a :exc:`RuntimeError` is raised. |
| 83 | |
| 84 | There is no return value. |
| 85 | |
| 86 | |
| 87 | .. class:: Event(\*, loop=None) |
| 88 | |
| 89 | An Event implementation, asynchronous equivalent to :class:`threading.Event`. |
| 90 | |
| 91 | Class implementing event objects. An event manages a flag that can be set to |
| 92 | true with the :meth:`set` method and reset to false with the :meth:`clear` |
| 93 | method. The :meth:`wait` method blocks until the flag is true. The flag is |
| 94 | initially false. |
| 95 | |
| 96 | .. method:: clear() |
| 97 | |
| 98 | Reset the internal flag to false. Subsequently, coroutines calling |
| 99 | :meth:`wait` will block until :meth:`set` is called to set the internal |
| 100 | flag to true again. |
| 101 | |
| 102 | .. method:: is_set() |
| 103 | |
| 104 | Return ``True`` if and only if the internal flag is true. |
| 105 | |
| 106 | .. method:: set() |
| 107 | |
| 108 | Set the internal flag to true. All coroutines waiting for it to become |
| 109 | true are awakened. Coroutine that call :meth:`wait` once the flag is true |
| 110 | will not block at all. |
| 111 | |
| 112 | .. method:: wait() |
| 113 | |
| 114 | Block until the internal flag is true. |
| 115 | |
| 116 | If the internal flag is true on entry, return ``True`` immediately. |
| 117 | Otherwise, block until another coroutine calls :meth:`set` to set the |
| 118 | flag to true, then return ``True``. |
| 119 | |
| 120 | This method returns a :ref:`coroutine <coroutine>`. |
| 121 | |
| 122 | |
| 123 | .. class:: Condition(\*, loop=None) |
| 124 | |
| 125 | A Condition implementation, asynchronous equivalent to |
| 126 | :class:`threading.Condition`. |
| 127 | |
| 128 | This class implements condition variable objects. A condition variable |
| 129 | allows one or more coroutines to wait until they are notified by another |
| 130 | coroutine. |
| 131 | |
| 132 | A new :class:`Lock` object is created and used as the underlying lock. |
| 133 | |
| 134 | .. method:: notify(n=1) |
| 135 | |
| 136 | By default, wake up one coroutine waiting on this condition, if any. |
| 137 | If the calling coroutine has not acquired the lock when this method is |
| 138 | called, a :exc:`RuntimeError` is raised. |
| 139 | |
| 140 | This method wakes up at most *n* of the coroutines waiting for the |
| 141 | condition variable; it is a no-op if no coroutines are waiting. |
| 142 | |
| 143 | .. note:: |
| 144 | |
| 145 | An awakened coroutine does not actually return from its :meth:`wait` |
| 146 | call until it can reacquire the lock. Since :meth:`notify` does not |
| 147 | release the lock, its caller should. |
| 148 | |
| 149 | .. method:: notify_all() |
| 150 | |
| 151 | Wake up all threads waiting on this condition. This method acts like |
| 152 | :meth:`notify`, but wakes up all waiting threads instead of one. If the |
| 153 | calling thread has not acquired the lock when this method is called, a |
| 154 | :exc:`RuntimeError` is raised. |
| 155 | |
| 156 | .. method:: wait() |
| 157 | |
| 158 | Wait until notified. |
| 159 | |
| 160 | If the calling coroutine has not acquired the lock when this method is |
| 161 | called, a :exc:`RuntimeError` is raised. |
| 162 | |
| 163 | This method releases the underlying lock, and then blocks until it is |
| 164 | awakened by a :meth:`notify` or :meth:`notify_all` call for the same |
| 165 | condition variable in another coroutine. Once awakened, it re-acquires |
| 166 | the lock and returns ``True``. |
| 167 | |
| 168 | This method returns a :ref:`coroutine <coroutine>`. |
| 169 | |
| 170 | .. method:: wait_for(predicate) |
| 171 | |
| 172 | Wait until a predicate becomes true. |
| 173 | |
| 174 | The predicate should be a callable which result will be interpreted as a |
| 175 | boolean value. The final predicate value is the return value. |
| 176 | |
| 177 | This method returns a :ref:`coroutine <coroutine>`. |
| 178 | |
| 179 | |
| 180 | Semaphores |
| 181 | ---------- |
| 182 | |
| 183 | .. class:: Semaphore(value=1, \*, loop=None) |
| 184 | |
| 185 | A Semaphore implementation. |
| 186 | |
| 187 | A semaphore manages an internal counter which is decremented by each |
| 188 | :meth:`acquire` call and incremented by each :meth:`release` call. The |
| 189 | counter can never go below zero; when :meth:`acquire` finds that it is zero, |
| 190 | it blocks, waiting until some other thread calls :meth:`release`. |
| 191 | |
| 192 | Semaphores also support the context manager protocol. |
| 193 | |
| 194 | The optional argument gives the initial value for the internal counter; it |
| 195 | defaults to ``1``. If the value given is less than ``0``, :exc:`ValueError` |
| 196 | is raised. |
| 197 | |
| 198 | .. method:: acquire() |
| 199 | |
| 200 | Acquire a semaphore. |
| 201 | |
| 202 | If the internal counter is larger than zero on entry, decrement it by one |
| 203 | and return ``True`` immediately. If it is zero on entry, block, waiting |
| 204 | until some other coroutine has called :meth:`release` to make it larger |
| 205 | than ``0``, and then return ``True``. |
| 206 | |
| 207 | This method returns a :ref:`coroutine <coroutine>`. |
| 208 | |
| 209 | .. method:: locked() |
| 210 | |
| 211 | Returns ``True`` if semaphore can not be acquired immediately. |
| 212 | |
| 213 | .. method:: release() |
| 214 | |
| 215 | Release a semaphore, incrementing the internal counter by one. When it |
| 216 | was zero on entry and another coroutine is waiting for it to become |
| 217 | larger than zero again, wake up that coroutine. |
| 218 | |
| 219 | |
| 220 | .. class:: BoundedSemaphore(value=1, \*, loop=None) |
| 221 | |
| 222 | A bounded semaphore implementation. Inherit from :class:`Semaphore`. |
| 223 | |
| 224 | This raises :exc:`ValueError` in :meth:`~Semaphore.release` if it would |
| 225 | increase the value above the initial value. |
| 226 | |
| 227 | |
| 228 | Queues |
| 229 | ------ |
| 230 | |
| 231 | .. class:: Queue(maxsize=0, \*, loop=None) |
| 232 | |
| 233 | A queue, useful for coordinating producer and consumer coroutines. |
| 234 | |
| 235 | If *maxsize* is less than or equal to zero, the queue size is infinite. If |
| 236 | it is an integer greater than ``0``, then ``yield from put()`` will block |
| 237 | when the queue reaches *maxsize*, until an item is removed by :meth:`get`. |
| 238 | |
| 239 | Unlike the standard library :mod:`queue`, you can reliably know this Queue's |
| 240 | size with :meth:`qsize`, since your single-threaded Tulip application won't |
| 241 | be interrupted between calling :meth:`qsize` and doing an operation on the |
| 242 | Queue. |
| 243 | |
| 244 | .. method:: empty() |
| 245 | |
| 246 | Return ``True`` if the queue is empty, ``False`` otherwise. |
| 247 | |
| 248 | .. method:: full() |
| 249 | |
| 250 | Return ``True`` if there are maxsize items in the queue. |
| 251 | |
| 252 | .. note:: |
| 253 | |
| 254 | If the Queue was initialized with ``maxsize=0`` (the default), then |
| 255 | :meth:`full()` is never ``True``. |
| 256 | |
| 257 | .. method:: get() |
| 258 | |
| 259 | Remove and return an item from the queue. |
| 260 | |
| 261 | If you yield from :meth:`get()`, wait until a item is available. |
| 262 | |
| 263 | This method returns a :ref:`coroutine <coroutine>`. |
| 264 | |
| 265 | .. method:: get_nowait() |
| 266 | |
| 267 | Remove and return an item from the queue. |
| 268 | |
| 269 | Return an item if one is immediately available, else raise |
| 270 | :exc:`~queue.Empty`. |
| 271 | |
| 272 | .. method:: put(item) |
| 273 | |
| 274 | Put an item into the queue. |
| 275 | |
| 276 | If you yield from ``put()``, wait until a free slot is available before |
| 277 | adding item. |
| 278 | |
| 279 | This method returns a :ref:`coroutine <coroutine>`. |
| 280 | |
| 281 | .. method:: put_nowait(item) |
| 282 | |
| 283 | Put an item into the queue without blocking. |
| 284 | |
| 285 | If no free slot is immediately available, raise :exc:`~queue.Full`. |
| 286 | |
| 287 | .. method:: qsize() |
| 288 | |
| 289 | Number of items in the queue. |
| 290 | |
| 291 | .. attribute:: maxsize |
| 292 | |
| 293 | Number of items allowed in the queue. |
| 294 | |
| 295 | |
| 296 | .. class:: PriorityQueue |
| 297 | |
| 298 | A subclass of :class:`Queue`; retrieves entries in priority order (lowest |
| 299 | first). |
| 300 | |
| 301 | Entries are typically tuples of the form: (priority number, data). |
| 302 | |
| 303 | |
| 304 | .. class:: LifoQueue |
| 305 | |
| 306 | A subclass of :class:`Queue` that retrieves most recently added entries |
| 307 | first. |
| 308 | |
| 309 | |
| 310 | .. class:: JoinableQueue |
| 311 | |
| 312 | A subclass of :class:`Queue` with :meth:`task_done` and :meth:`join` |
| 313 | methods. |
| 314 | |
| 315 | .. method:: join() |
| 316 | |
| 317 | Block until all items in the queue have been gotten and processed. |
| 318 | |
| 319 | The count of unfinished tasks goes up whenever an item is added to the |
| 320 | queue. The count goes down whenever a consumer thread calls |
| 321 | :meth:`task_done` to indicate that the item was retrieved and all work on |
| 322 | it is complete. When the count of unfinished tasks drops to zero, |
| 323 | :meth:`join` unblocks. |
| 324 | |
| 325 | This method returns a :ref:`coroutine <coroutine>`. |
| 326 | |
| 327 | .. method:: task_done() |
| 328 | |
| 329 | Indicate that a formerly enqueued task is complete. |
| 330 | |
| 331 | Used by queue consumers. For each :meth:`~Queue.get` used to fetch a task, a |
| 332 | subsequent call to :meth:`task_done` tells the queue that the processing |
| 333 | on the task is complete. |
| 334 | |
| 335 | If a :meth:`join` is currently blocking, it will resume when all items |
| 336 | have been processed (meaning that a :meth:`task_done` call was received |
| 337 | for every item that had been :meth:`~Queue.put` into the queue). |
| 338 | |
| 339 | Raises :exc:`ValueError` if called more times than there were items |
| 340 | placed in the queue. |
| 341 | |