Movatterモバイル変換


[0]ホーム

URL:


Up one LevelPython Library ReferenceContentsModule IndexIndex

15.3threading -- Higher-level threading interface

This module constructs higher-level threading interfaces on top of the lower levelthread module.

Thedummy_threading module is provided forsituations wherethreading cannot be used becausethread is missing.

This module defines the following functions and objects:

activeCount()
Return the number ofThread objects currently alive. Thereturned count is equal to the length of the list returned byenumerate().

Condition()
A factory function that returns a new condition variable object.A condition variable allows one or more threads to wait until theyare notified by another thread.

currentThread()
Return the currentThread object, corresponding to thecaller's thread of control. If the caller's thread of control was notcreated through thethreading module, a dummy thread object with limited functionalityis returned.

enumerate()
Return a list of allThread objects currently alive. The listincludes daemonic threads, dummy thread objects created bycurrentThread(), and the main thread. It excludesterminated threads and threads that have not yet been started.

Event()
A factory function that returns a new event object. An event managesa flag that can be set to true with theset() method andreset to false with theclear() method. Thewait()method blocks until the flag is true.

class local
A class that represents thread-local data. Thread-local data are datawhose values are thread specific. To manage thread-local data, justcreate an instance oflocal (or a subclass) and storeattributes on it:

mydata = threading.local()mydata.x = 1

The instance's values will be different for separate threads.

For more details and extensive examples, see the documentation stringof the_threading_local module.

New in version 2.4.

Lock()
A factory function that returns a new primitive lock object. Oncea thread has acquired it, subsequent attempts to acquire it block,until it is released; any thread may release it.

RLock()
A factory function that returns a new reentrant lock object.A reentrant lock must be released by the thread that acquired it.Once a thread has acquired a reentrant lock, the same thread mayacquire it again without blocking; the thread must release it oncefor each time it has acquired it.

Semaphore([value])
A factory function that returns a new semaphore object. Asemaphore manages a counter representing the number ofrelease()calls minus the number ofacquire() calls, plus an initial value.Theacquire() method blocks if necessary until it can returnwithout making the counter negative. If not given,value defaults to1.

BoundedSemaphore([value])
A factory function that returns a new bounded semaphore object. A boundedsemaphore checks to make sure its current value doesn't exceed its initialvalue. If it does,ValueError is raised. In most situationssemaphores are used to guard resources with limited capacity. If thesemaphore is released too many times it's a sign of a bug. If not given,value defaults to 1.

class Thread
A class that represents a thread of control. This class can be safelysubclassed in a limited fashion.

class Timer
A thread that executes a function after a specified interval has passed.

settrace(func)
Set a trace function for all threads startedfrom thethreading module. Thefunc will be passed tosys.settrace() for each thread, before itsrun()method is called.New in version 2.3.

setprofile(func)
Set a profile function for all threads startedfrom thethreading module. Thefunc will be passed tosys.setprofile() for each thread, before itsrun()method is called.New in version 2.3.

stack_size([size])
Return the thread stack size used when creating new threads. Theoptionalsize argument specifies the stack size to be used forsubsequently created threads, and must be 0 (use platform orconfigured default) or a positive integer value of at least 32,768 (32kB).If changing the thread stack size is unsupported, aThreadErroris raised. If the specified stack size is invalid, aValueErroris raised and the stack size is unmodified. 32kB is currently the minimumsupported stack size value to guarantee sufficient stack space for theinterpreter itself. Note that some platforms may have particularrestrictions on values for the stack size, such as requiring a minimumstack size > 32kB or requiring allocation in multiples of the systemmemory page size - platform documentation should be referred to formore information (4kB pages are common; using multiples of 4096 forthe stack size is the suggested approach in the absence of morespecific information).Availability: Windows, systems with POSIX threads.New in version 2.5.

Detailed interfaces for the objects are documented below.

The design of this module is loosely based on Java's threading model.However, where Java makes locks and condition variables basic behaviorof every object, they are separate objects in Python. Python'sThreadclass supports a subset of the behavior of Java's Thread class;currently, there are no priorities, no thread groups, and threadscannot be destroyed, stopped, suspended, resumed, or interrupted. Thestatic methods of Java's Thread class, when implemented, are mapped tomodule-level functions.

All of the methods described below are executed atomically.



Subsections


Up one LevelPython Library ReferenceContentsModule IndexIndex

Release 2.5.2, documentation updated on 21st February, 2008.
SeeAbout this document... for information on suggesting changes.
[8]ページ先頭

©2009-2025 Movatter.jp