Lines Matching +full:hook +full:- +full:run +full:- +full:in +full:- +full:context
1 :mod:`threading` --- Thread-based parallelism
5 :synopsis: Thread-based parallelism.
9 --------------
11 This module constructs higher-level threading interfaces on top of the lower
19 In the Python 2.x series, this module contained ``camelCase`` names
24 .. impl-detail::
26 In CPython, due to the :term:`Global Interpreter Lock
28 can execute Python code at once (even though certain performance-oriented
31 resources of multi-core machines, you are advised to use
33 However, threading is still an appropriate model if you want to run
34 multiple I/O-bound tasks simultaneously.
60 Handle uncaught exception raised by :func:`Thread.run`.
76 exceptions raised by :func:`Thread.run` are handled.
78 Storing *exc_value* using a custom hook can create a reference cycle. It
82 Storing *thread* using a custom hook can resurrect it if it is set to an
84 hook completes to avoid resurrecting objects.
94 original value can be restored in case they happen to get replaced with
103 to be used e.g. to index a dictionary of thread-specific data. Thread
113 This is a non-negative integer.
114 Its value may be used to uniquely identify this particular thread system-wide
133 Return the main :class:`Thread` object. In normal conditions, the
146 :meth:`~Thread.run` method is called.
166 :meth:`~Thread.run` method is called.
190 minimum stack size > 32 KiB or requiring allocation in multiples of the system
191 memory page size - platform documentation should be referred to for more
193 the suggested approach in the absence of more specific information).
210 This module defines a number of classes, which are detailed in the sections
215 they are separate objects in Python. Python's :class:`Thread` class supports a
219 when implemented, are mapped to module-level functions.
224 Thread-Local Data
225 -----------------
227 Thread-local data is data whose values are thread specific. To manage
228 thread-local data, just create an instance of :class:`local` (or a
239 A class that represents thread-local data.
245 .. _thread-objects:
248 --------------
250 The :class:`Thread` class represents an activity that is run in a separate
252 callable object to the constructor, or by overriding the :meth:`~Thread.run`
253 method in a subclass. No other methods (except for the constructor) should be
254 overridden in a subclass. In other words, *only* override the
255 :meth:`~Thread.__init__` and :meth:`~Thread.run` methods of this class.
258 thread's :meth:`~Thread.start` method. This invokes the :meth:`~Thread.run`
259 method in a separate thread of control.
262 stops being alive when its :meth:`~Thread.run` method terminates -- either
273 If the :meth:`~Thread.run` method raises an exception,
286 If you want your threads to stop gracefully, make them non-daemonic and
290 control in the Python program. It is not a daemon thread.
309 *target* is the callable object to be invoked by the :meth:`run` method.
313 of the form "Thread-*N*" where *N* is a small decimal number,
314 or "Thread-*N* (target)" where "target" is ``target.__name__`` if the
341 object's :meth:`~Thread.run` method to be invoked in a separate thread
347 .. method:: run()
351 You may override this method in a subclass. The standard :meth:`run`
359 the thread whose :meth:`~Thread.join` method is called terminates -- either
360 normally or through an unhandled exception -- or until the optional
364 floating point number specifying a timeout for the operation in seconds
367 decide whether a timeout happened -- if the thread is still alive, the
405 This is a non-negative integer, or ``None`` if the thread has not
408 system-wide (until the thread terminates, after which the value
414 system-wide) from the time the thread is created until the thread
425 This method returns ``True`` just before the :meth:`~Thread.run` method
426 starts until just after the :meth:`~Thread.run` method terminates. The
435 therefore all threads created in the main thread default to
438 The entire Python program exits when no alive non-daemon threads are left.
449 .. _lock-objects:
452 ------------
455 particular thread when locked. In Python, it is currently the lowest level
459 A primitive lock is in one of two states, "locked" or "unlocked". It is created
460 in the unlocked state. It has two basic methods, :meth:`~Lock.acquire` and
463 :meth:`~Lock.acquire` blocks until a call to :meth:`~Lock.release` in another
466 called in the locked state; it changes the state to unlocked and returns
470 Locks also support the :ref:`context management protocol <with-locks>`.
472 When more than one thread is blocked in :meth:`~Lock.acquire` waiting for the
491 .. method:: acquire(blocking=True, timeout=-1)
493 Acquire a lock, blocking or non-blocking.
502 When invoked with the floating-point *timeout* argument set to a positive
504 and as long as the lock cannot be acquired. A *timeout* argument of ``-1``
538 .. _rlock-objects:
541 -------------
545 and "recursion level" in addition to the locked/unlocked state used by primitive
546 locks. In the locked state, some thread owns the lock; in the unlocked state,
554 allows another thread blocked in :meth:`~Lock.acquire` to proceed.
556 Reentrant locks also support the :ref:`context management protocol <with-locks>`.
571 .. method:: acquire(blocking=True, timeout=-1)
573 Acquire a lock, blocking or non-blocking.
581 There is no return value in this case.
590 When invoked with the floating-point *timeout* argument set to a positive
614 .. _condition-objects:
617 -----------------
620 passed in or one will be created by default. Passing one in is useful when
624 A condition variable obeys the :ref:`context management protocol <with-locks>`:
634 re-acquires the lock and returns. It is also possible to specify a timeout.
647 synchronize access to some shared state; threads that are interested in a
651 the state in such a way that it could possibly be a desired state for one
653 producer-consumer situation with unlimited buffer capacity::
669 no longer hold true. This is inherent to multi-threaded programming. The
680 waiting threads. E.g. in a typical producer-consumer situation, adding one
714 condition variable in another thread, or until the optional timeout
715 occurs. Once awakened or timed out, it re-acquires the lock and returns.
718 floating point number specifying a timeout for the operation in seconds
729 The return value is ``True`` unless a given *timeout* expired, in which
753 held when called and is re-acquired on return. The predicate is evaluated
765 variable; it is a no-op if no threads are waiting.
786 .. _semaphore-objects:
789 -----------------
791 This is one of the oldest synchronization primitives in the history of computer
802 Semaphores also support the :ref:`context management protocol <with-locks>`.
832 order in which threads are awoken should not be relied on.
839 most *timeout* seconds. If acquire does not complete successfully in
859 :exc:`ValueError` is raised. In most situations semaphores are used to guard
867 .. _semaphore-examples:
873 a database server. In any situation where the size of the resource is fixed,
895 .. _event-objects:
898 -------------
943 floating point number specifying a timeout for the operation in seconds
955 .. _timer-objects:
958 -------------
960 This class represents an action that should be run only after a certain amount
961 of time has passed --- a timer. :class:`Timer` is a subclass of :class:`Thread`
981 Create a timer that will run *function* with arguments *args* and keyword
992 only work if the timer is still in its waiting stage.
996 ---------------
1037 provided, it is used in preference to any that was supplied to the class
1040 The return value is an integer in the range 0 to *parties* -- 1, different
1084 The number of threads currently waiting in the barrier.
1088 A boolean that is ``True`` if the barrier is in the broken state.
1097 .. _with-locks:
1099 Using locks, conditions, and semaphores in the :keyword:`!with` statement
1100 -------------------------------------------------------------------------
1103 :meth:`release` methods can be used as context managers for a :keyword:`with`
1121 :keyword:`with` statement context managers.