• Home
  • Raw
  • Download

Lines Matching full:thread

1 :mod:`threading` --- Thread-based parallelism
5 :synopsis: Thread-based parallelism.
20 to push tasks to a background thread without blocking execution of the
21 calling thread, while still being able to retrieve their results when needed.
23 :mod:`queue` provides a thread-safe interface for exchanging data between
39 <global interpreter lock>`, only one thread
55 Return the number of :class:`Thread` objects currently alive. The returned
63 Return the current :class:`Thread` object, corresponding to the caller's thread
64 of control. If the caller's thread of control was not created through the
65 :mod:`threading` module, a dummy thread object with limited functionality is
73 Handle uncaught exception raised by :func:`Thread.run`.
80 * *thread*: Thread which raised the exception, can be ``None``.
89 exceptions raised by :func:`Thread.run` are handled.
95 Storing *thread* using a custom hook can resurrect it if it is set to an
96 object which is being finalized. Avoid storing *thread* after the custom
114 Return the 'thread identifier' of the current thread. This is a nonzero
116 to be used e.g. to index a dictionary of thread-specific data. Thread
117 identifiers may be recycled when a thread exits and another thread is
125 Return the native integral Thread ID of the current thread assigned by the kernel.
127 Its value may be used to uniquely identify this particular thread system-wide
128 (until the thread terminates, after which the value may be recycled by the OS).
137 Return a list of all :class:`Thread` objects currently active. The list
138 includes daemonic threads and dummy thread objects created by
140 that have not yet been started. However, the main thread is always part
146 Return the main :class:`Thread` object. In normal conditions, the
147 main thread is the thread from which the Python interpreter was
158 The *func* will be passed to :func:`sys.settrace` for each thread, before its
159 :meth:`~Thread.run` method is called.
178 The *func* will be passed to :func:`sys.setprofile` for each thread, before its
179 :meth:`~Thread.run` method is called.
193 Return the thread stack size used when creating new threads. The optional
197 0 is used. If changing the thread stack size is
230 they are separate objects in Python. Python's :class:`Thread` class supports a
231 subset of the behavior of Java's Thread class; currently, there are no
232 priorities, no thread groups, and threads cannot be destroyed, stopped,
233 suspended, resumed, or interrupted. The static methods of Java's Thread class,
239 Thread-Local Data
242 Thread-local data is data whose values are thread specific. To manage
243 thread-local data, just create an instance of :class:`local` (or a
254 A class that represents thread-local data.
262 Thread Objects
265 The :class:`Thread` class represents an activity that is run in a separate
266 thread of control. There are two ways to specify the activity: by passing a
267 callable object to the constructor, or by overriding the :meth:`~Thread.run`
270 :meth:`~Thread.__init__` and :meth:`~Thread.run` methods of this class.
272 Once a thread object is created, its activity must be started by calling the
273 thread's :meth:`~Thread.start` method. This invokes the :meth:`~Thread.run`
274 method in a separate thread of control.
276 Once the thread's activity is started, the thread is considered 'alive'. It
277 stops being alive when its :meth:`~Thread.run` method terminates -- either
278 normally, or by raising an unhandled exception. The :meth:`~Thread.is_alive`
279 method tests whether the thread is alive.
281 Other threads can call a thread's :meth:`~Thread.join` method. This blocks
282 the calling thread until the thread whose :meth:`~Thread.join` method is
285 A thread has a name. The name can be passed to the constructor, and read or
286 changed through the :attr:`~Thread.name` attribute.
288 If the :meth:`~Thread.run` method raises an exception,
292 A thread can be flagged as a "daemon thread". The significance of this flag is
294 initial value is inherited from the creating thread. The flag can be set
295 through the :attr:`~Thread.daemon` property or the *daemon* constructor
304 There is a "main thread" object; this corresponds to the initial thread of
305 control in the Python program. It is not a daemon thread.
307 There is the possibility that "dummy thread objects" are created. These are
308 thread objects corresponding to "alien threads", which are threads of control
310 thread objects have limited functionality; they are always considered alive and
311 daemonic, and cannot be :ref:`joined <meth-thread-join>`. They are never deleted,
315 .. class:: Thread(group=None, target=None, name=None, args=(), kwargs={}, *, \
327 *name* is the thread name. By default, a unique name is constructed
328 of the form "Thread-*N*" where *N* is a small decimal number,
329 or "Thread-*N* (target)" where "target" is ``target.__name__`` if the
337 If not ``None``, *daemon* explicitly sets whether the thread is daemonic.
339 current thread.
342 base class constructor (``Thread.__init__()``) before doing anything else to
343 the thread.
353 Start the thread's activity.
355 It must be called at most once per thread object. It arranges for the
356 object's :meth:`~Thread.run` method to be invoked in a separate thread
360 on the same thread object.
364 Method representing the thread's activity.
371 Using list or tuple as the *args* argument which passed to the :class:`Thread`
376 >>> from threading import Thread
377 >>> t = Thread(target=print, args=[1])
380 >>> t = Thread(target=print, args=(1,))
384 .. _meth-thread-join:
388 Wait until the thread terminates. This blocks the calling thread until
389 the thread whose :meth:`~Thread.join` method is called terminates -- either
395 (or fractions thereof). As :meth:`~Thread.join` always returns ``None``,
396 you must call :meth:`~Thread.is_alive` after :meth:`~Thread.join` to
397 decide whether a timeout happened -- if the thread is still alive, the
398 :meth:`~Thread.join` call timed out.
401 block until the thread terminates.
403 A thread can be joined many times.
405 :meth:`~Thread.join` raises a :exc:`RuntimeError` if an attempt is made
406 to join the current thread as that would cause a deadlock. It is also
407 an error to :meth:`~Thread.join` a thread before it has been started
419 Deprecated getter/setter API for :attr:`~Thread.name`; use it directly as a
426 The 'thread identifier' of this thread or ``None`` if the thread has not
428 function. Thread identifiers may be recycled when a thread exits and
429 another thread is created. The identifier is available even after the
430 thread has exited.
434 The Thread ID (``TID``) of this thread, as assigned by the OS (kernel).
435 This is a non-negative integer, or ``None`` if the thread has not
437 This value may be used to uniquely identify this particular thread
438 system-wide (until the thread terminates, after which the value
443 Similar to Process IDs, Thread IDs are only valid (guaranteed unique
444 system-wide) from the time the thread is created until the thread
453 Return whether the thread is alive.
455 This method returns ``True`` just before the :meth:`~Thread.run` method
456 starts until just after the :meth:`~Thread.run` method terminates. The
461 A boolean value indicating whether this thread is a daemon thread (``True``)
462 or not (``False``). This must be set before :meth:`~Thread.start` is called,
464 from the creating thread; the main thread is not a daemon thread and
465 therefore all threads created in the main thread default to
466 :attr:`~Thread.daemon` = ``False``.
473 Deprecated getter/setter API for :attr:`~Thread.daemon`; use it directly as a
485 particular thread when locked. In Python, it is currently the lowest level
494 thread changes it to unlocked, then the :meth:`~Lock.acquire` call resets it
502 When more than one thread is blocked in :meth:`~Lock.acquire` waiting for the
503 state to turn to unlocked, only one thread proceeds when a :meth:`~Lock.release`
512 The class implementing primitive lock objects. Once a thread has acquired a
514 thread may release it.
551 Release a lock. This can be called from any thread, not only the thread
574 times by the same thread. Internally, it uses the concepts of "owning thread"
576 locks. In the locked state, some thread owns the lock; in the unlocked state,
577 no thread owns it.
579 To lock the lock, a thread calls its :meth:`~RLock.acquire` method; this
580 returns once the thread owns the lock. To unlock the lock, a thread calls
584 allows another thread blocked in :meth:`~Lock.acquire` to proceed.
592 released by the thread that acquired it. Once a thread has acquired a
593 reentrant lock, the same thread may acquire it again without blocking; the
594 thread must release it once for each time it has acquired it.
605 When invoked without arguments: if this thread already owns the lock, increment
607 thread owns the lock, block until the lock is unlocked. Once the lock is
608 unlocked (not owned by any thread), then grab ownership, set the recursion level
609 to one, and return. If more than one thread is blocked waiting until the lock
632 zero, reset the lock to unlocked (not owned by any thread), and if any other
635 nonzero, the lock remains locked and owned by the calling thread.
637 Only call this method when the calling thread owns the lock. A
662 another thread awakens it by calling :meth:`~Condition.notify` or
671 don't release the lock; this means that the thread or threads awakened will
673 the thread that called :meth:`~Condition.notify` or :meth:`~Condition.notify_all`
711 item to the buffer only needs to wake up one consumer thread.
717 allows one or more threads to wait until they are notified by another thread.
738 Wait until notified or until a timeout occurs. If the calling thread has
744 condition variable in another thread, or until the optional timeout
790 By default, wake up one thread waiting on this condition, if any. If the
791 calling thread has not acquired the lock when this method is called, a
802 Note: an awakened thread does not actually return from its :meth:`wait`
810 calling thread has not acquired the lock when this method is called, a
829 finds that it is zero, it blocks, waiting until some other thread calls
861 thread will be awoken by each call to :meth:`~Semaphore.release`. The
905 main thread would initialize the semaphore::
931 thread signals an event and other threads wait for it.
969 entry, return immediately. Otherwise, block until another thread calls
991 of time has passed --- a timer. :class:`Timer` is a subclass of :class:`Thread`
1038 As an example, here is a simple way to synchronize a client and server thread::
1071 for each thread. This can be used to select a thread to do some special
1076 # Only one thread needs to print this
1086 barrier is broken or reset while a thread is waiting.