Lines Matching full:thread
1 :mod:`threading` --- Thread-based parallelism
5 :synopsis: Thread-based parallelism.
29 Return the number of :class:`Thread` objects currently alive. The returned
35 Return the current :class:`Thread` object, corresponding to the caller's thread
36 of control. If the caller's thread of control was not created through the
37 :mod:`threading` module, a dummy thread object with limited functionality is
43 Handle uncaught exception raised by :func:`Thread.run`.
50 * *thread*: Thread which raised the exception, can be ``None``.
59 exceptions raised by :func:`Thread.run` are handled.
65 Storing *thread* using a custom hook can resurrect it if it is set to an
66 object which is being finalized. Avoid storing *thread* after the custom
77 Return the 'thread identifier' of the current thread. This is a nonzero
79 to be used e.g. to index a dictionary of thread-specific data. Thread
80 identifiers may be recycled when a thread exits and another thread is
88 Return the native integral Thread ID of the current thread assigned by the kernel.
90 Its value may be used to uniquely identify this particular thread system-wide
91 (until the thread terminates, after which the value may be recycled by the OS).
100 Return a list of all :class:`Thread` objects currently alive. The list
101 includes daemonic threads, dummy thread objects created by
102 :func:`current_thread`, and the main thread. It excludes terminated threads
108 Return the main :class:`Thread` object. In normal conditions, the
109 main thread is the thread from which the Python interpreter was
120 The *func* will be passed to :func:`sys.settrace` for each thread, before its
121 :meth:`~Thread.run` method is called.
129 The *func* will be passed to :func:`sys.setprofile` for each thread, before its
130 :meth:`~Thread.run` method is called.
135 Return the thread stack size used when creating new threads. The optional
139 0 is used. If changing the thread stack size is
170 they are separate objects in Python. Python's :class:`Thread` class supports a
171 subset of the behavior of Java's Thread class; currently, there are no
172 priorities, no thread groups, and threads cannot be destroyed, stopped,
173 suspended, resumed, or interrupted. The static methods of Java's Thread class,
179 Thread-Local Data
182 Thread-local data is data whose values are thread specific. To manage
183 thread-local data, just create an instance of :class:`local` (or a
194 A class that represents thread-local data.
202 Thread Objects
205 The :class:`Thread` class represents an activity that is run in a separate
206 thread of control. There are two ways to specify the activity: by passing a
207 callable object to the constructor, or by overriding the :meth:`~Thread.run`
210 :meth:`~Thread.__init__` and :meth:`~Thread.run` methods of this class.
212 Once a thread object is created, its activity must be started by calling the
213 thread's :meth:`~Thread.start` method. This invokes the :meth:`~Thread.run`
214 method in a separate thread of control.
216 Once the thread's activity is started, the thread is considered 'alive'. It
217 stops being alive when its :meth:`~Thread.run` method terminates -- either
218 normally, or by raising an unhandled exception. The :meth:`~Thread.is_alive`
219 method tests whether the thread is alive.
221 Other threads can call a thread's :meth:`~Thread.join` method. This blocks
222 the calling thread until the thread whose :meth:`~Thread.join` method is
225 A thread has a name. The name can be passed to the constructor, and read or
226 changed through the :attr:`~Thread.name` attribute.
228 If the :meth:`~Thread.run` method raises an exception,
232 A thread can be flagged as a "daemon thread". The significance of this flag is
234 initial value is inherited from the creating thread. The flag can be set
235 through the :attr:`~Thread.daemon` property or the *daemon* constructor
244 There is a "main thread" object; this corresponds to the initial thread of
245 control in the Python program. It is not a daemon thread.
247 There is the possibility that "dummy thread objects" are created. These are
248 thread objects corresponding to "alien threads", which are threads of control
250 thread objects have limited functionality; they are always considered alive and
251 daemonic, and cannot be :meth:`~Thread.join`\ ed. They are never deleted,
255 .. class:: Thread(group=None, target=None, name=None, args=(), kwargs={}, *, \
267 *name* is the thread name. By default, a unique name is constructed of the
268 form "Thread-*N*" where *N* is a small decimal number.
275 If not ``None``, *daemon* explicitly sets whether the thread is daemonic.
277 current thread.
280 base class constructor (``Thread.__init__()``) before doing anything else to
281 the thread.
288 Start the thread's activity.
290 It must be called at most once per thread object. It arranges for the
291 object's :meth:`~Thread.run` method to be invoked in a separate thread
295 on the same thread object.
299 Method representing the thread's activity.
308 Wait until the thread terminates. This blocks the calling thread until
309 the thread whose :meth:`~Thread.join` method is called terminates -- either
315 (or fractions thereof). As :meth:`~Thread.join` always returns ``None``,
316 you must call :meth:`~Thread.is_alive` after :meth:`~Thread.join` to
317 decide whether a timeout happened -- if the thread is still alive, the
318 :meth:`~Thread.join` call timed out.
321 block until the thread terminates.
323 A thread can be :meth:`~Thread.join`\ ed many times.
325 :meth:`~Thread.join` raises a :exc:`RuntimeError` if an attempt is made
326 to join the current thread as that would cause a deadlock. It is also
327 an error to :meth:`~Thread.join` a thread before it has been started
339 Old getter/setter API for :attr:`~Thread.name`; use it directly as a
344 The 'thread identifier' of this thread or ``None`` if the thread has not
346 function. Thread identifiers may be recycled when a thread exits and
347 another thread is created. The identifier is available even after the
348 thread has exited.
352 The native integral thread ID of this thread.
353 This is a non-negative integer, or ``None`` if the thread has not
355 This represents the Thread ID (``TID``) as assigned to the
356 thread by the OS (kernel). Its value may be used to uniquely identify
357 this particular thread system-wide (until the thread terminates,
362 Similar to Process IDs, Thread IDs are only valid (guaranteed unique
363 system-wide) from the time the thread is created until the thread
372 Return whether the thread is alive.
374 This method returns ``True`` just before the :meth:`~Thread.run` method
375 starts until just after the :meth:`~Thread.run` method terminates. The
380 A boolean value indicating whether this thread is a daemon thread (True)
381 or not (False). This must be set before :meth:`~Thread.start` is called,
383 from the creating thread; the main thread is not a daemon thread and
384 therefore all threads created in the main thread default to
385 :attr:`~Thread.daemon` = ``False``.
392 Old getter/setter API for :attr:`~Thread.daemon`; use it directly as a
398 In CPython, due to the :term:`Global Interpreter Lock`, only one thread
414 particular thread when locked. In Python, it is currently the lowest level
423 thread changes it to unlocked, then the :meth:`~Lock.acquire` call resets it
431 When more than one thread is blocked in :meth:`~Lock.acquire` waiting for the
432 state to turn to unlocked, only one thread proceeds when a :meth:`~Lock.release`
441 The class implementing primitive lock objects. Once a thread has acquired a
443 thread may release it.
480 Release a lock. This can be called from any thread, not only the thread
502 times by the same thread. Internally, it uses the concepts of "owning thread"
504 locks. In the locked state, some thread owns the lock; in the unlocked state,
505 no thread owns it.
507 To lock the lock, a thread calls its :meth:`~RLock.acquire` method; this
508 returns once the thread owns the lock. To unlock the lock, a thread calls
512 allows another thread blocked in :meth:`~Lock.acquire` to proceed.
520 released by the thread that acquired it. Once a thread has acquired a
521 reentrant lock, the same thread may acquire it again without blocking; the
522 thread must release it once for each time it has acquired it.
533 When invoked without arguments: if this thread already owns the lock, increment
535 thread owns the lock, block until the lock is unlocked. Once the lock is
536 unlocked (not owned by any thread), then grab ownership, set the recursion level
537 to one, and return. If more than one thread is blocked waiting until the lock
560 zero, reset the lock to unlocked (not owned by any thread), and if any other
563 nonzero, the lock remains locked and owned by the calling thread.
565 Only call this method when the calling thread owns the lock. A
590 another thread awakens it by calling :meth:`~Condition.notify` or
599 don't release the lock; this means that the thread or threads awakened will
601 the thread that called :meth:`~Condition.notify` or :meth:`~Condition.notify_all`
639 item to the buffer only needs to wake up one consumer thread.
645 allows one or more threads to wait until they are notified by another thread.
666 Wait until notified or until a timeout occurs. If the calling thread has
672 condition variable in another thread, or until the optional timeout
718 By default, wake up one thread waiting on this condition, if any. If the
719 calling thread has not acquired the lock when this method is called, a
730 Note: an awakened thread does not actually return from its :meth:`wait`
738 calling thread has not acquired the lock when this method is called, a
755 finds that it is zero, it blocks, waiting until some other thread calls
787 thread will be awoken by each call to :meth:`~Semaphore.release`. The
804 was zero on entry and another thread is waiting for it to become larger
805 than zero again, wake up that thread.
828 main thread would initialize the semaphore::
854 thread signals an event and other threads wait for it.
890 entry, return immediately. Otherwise, block until another thread calls
912 of time has passed --- a timer. :class:`Timer` is a subclass of :class:`Thread`
959 As an example, here is a simple way to synchronize a client and server thread::
992 for each thread. This can be used to select a thread to do some special
997 # Only one thread needs to print this
1007 barrier is broken or reset while a thread is waiting.