Module threading
[hide private]
[frames] | no frames]

Module threading

source code

Thread module emulating a subset of Java's threading model.

Classes [hide private]
  Thread
A class that represents a thread of control.
  _BoundedSemaphore
A bounded semaphore checks to make sure its current value doesn't exceed its initial value.
  _Condition
Condition variables allow one or more threads to wait until they are notified by another thread.
  _DummyThread
  _Event
A factory function that returns a new event object.
  _MainThread
  _RLock
A reentrant lock must be released by the thread that acquired it.
  _Semaphore
Semaphores manage a counter representing the number of release() calls minus the number of acquire() calls, plus an initial value.
  _Timer
Call a function after a specified number of seconds:
  _Verbose
  local
Thread-local data
Functions [hide private]
 
BoundedSemaphore(*args, **kwargs)
A factory function that returns a new bounded semaphore.
source code
 
Condition(*args, **kwargs)
Factory function that returns a new condition variable object.
source code
 
Event(*args, **kwargs)
A factory function that returns a new event.
source code
lock object
Lock()
(allocate() is an obsolete synonym)
 
RLock(*args, **kwargs)
Factory function that returns a new reentrant lock.
source code
 
Semaphore(*args, **kwargs)
A factory function that returns a new semaphore.
source code
 
Timer(*args, **kwargs)
Factory function to create a Timer object.
source code
 
_after_fork() source code
 
_enumerate() source code
 
_newname(template='Thread-%d') source code
 
_pickSomeNonDaemonThread() source code
 
_shutdown() source code
 
_test() source code
 
activeCount()
Return the number of Thread objects currently alive.
source code
 
active_count()
Return the number of Thread objects currently alive.
source code
 
currentThread()
Return the current Thread object, corresponding to the caller's thread of control.
source code
 
current_thread()
Return the current Thread object, corresponding to the caller's thread of control.
source code
 
enumerate()
Return a list of all Thread objects currently alive.
source code
 
setprofile(func)
Set a profile function for all threads started from the threading module.
source code
 
settrace(func)
Set a trace function for all threads started from the threading module.
source code
size
stack_size(size=...)
Return the thread stack size used when creating new threads.
Variables [hide private]
  _VERBOSE = False
  __package__ = None
hash(x)
  _active = {139673421911936: <_MainThread(MainThread, started 1...
  _active_limbo_lock = <thread.lock object at 0x7f08186f90b0>
  _counter = <method-wrapper 'next' of itertools.count object at...
  _limbo = {}
  _profile_hook = None
hash(x)
  _trace_hook = None
hash(x)
Function Details [hide private]

BoundedSemaphore(*args, **kwargs)

source code 

A factory function that returns a new bounded semaphore.

A bounded semaphore checks to make sure its current value doesn't exceed its initial value. If it does, ValueError is raised. In most situations semaphores are used to guard resources with limited capacity.

If the semaphore is released too many times it's a sign of a bug. If not given, value defaults to 1.

Like regular semaphores, bounded semaphores manage a counter representing the number of release() calls minus the number of acquire() calls, plus an initial value. The acquire() method blocks if necessary until it can return without making the counter negative. If not given, value defaults to 1.

Condition(*args, **kwargs)

source code 

Factory function that returns a new condition variable object.

A condition variable allows one or more threads to wait until they are notified by another thread.

If the lock argument is given and not None, it must be a Lock or RLock object, and it is used as the underlying lock. Otherwise, a new RLock object is created and used as the underlying lock.

Event(*args, **kwargs)

source code 

A factory function that returns a new event.

Events manage a flag that can be set to true with the set() method and reset to false with the clear() method. The wait() method blocks until the flag is true.

Lock()

 

(allocate() is an obsolete synonym)

Create a new lock object. See help(LockType) for information about locks.

Returns: lock object

RLock(*args, **kwargs)

source code 

Factory function that returns a new reentrant lock.

A reentrant lock must be released by the thread that acquired it. Once a thread has acquired a reentrant lock, the same thread may acquire it again without blocking; the thread must release it once for each time it has acquired it.

Semaphore(*args, **kwargs)

source code 

A factory function that returns a new semaphore.

Semaphores manage a counter representing the number of release() calls minus the number of acquire() calls, plus an initial value. The acquire() method blocks if necessary until it can return without making the counter negative. If not given, value defaults to 1.

Timer(*args, **kwargs)

source code 
Factory function to create a Timer object.

Timers call a function after a specified number of seconds:

    t = Timer(30.0, f, args=[], kwargs={})
    t.start()
    t.cancel()     # stop the timer's action if it's still waiting

activeCount()

source code 

Return the number of Thread objects currently alive.

The returned count is equal to the length of the list returned by enumerate().

active_count()

source code 

Return the number of Thread objects currently alive.

The returned count is equal to the length of the list returned by enumerate().

currentThread()

source code 

Return the current Thread object, corresponding to the caller's thread of control.

If the caller's thread of control was not created through the threading module, a dummy thread object with limited functionality is returned.

current_thread()

source code 

Return the current Thread object, corresponding to the caller's thread of control.

If the caller's thread of control was not created through the threading module, a dummy thread object with limited functionality is returned.

enumerate()

source code 

Return a list of all Thread objects currently alive.

The list includes daemonic threads, dummy thread objects created by current_thread(), and the main thread. It excludes terminated threads and threads that have not yet been started.

setprofile(func)

source code 

Set a profile function for all threads started from the threading module.

The func will be passed to sys.setprofile() for each thread, before its run() method is called.

settrace(func)

source code 

Set a trace function for all threads started from the threading module.

The func will be passed to sys.settrace() for each thread, before its run() method is called.

stack_size(size=...)

 
Return the thread stack size used when creating new threads.  The
optional size argument specifies the stack size (in bytes) to be used
for subsequently created threads, and must be 0 (use platform or
configured default) or a positive integer value of at least 32,768 (32k).
If changing the thread stack size is unsupported, a ThreadError
exception is raised.  If the specified size is invalid, a ValueError
exception is raised, and the stack size is unmodified.  32k bytes
 currently the minimum supported stack size value to guarantee
sufficient stack space for the interpreter itself.

Note that some platforms may have particular restrictions on values for
the stack size, such as requiring a minimum stack size larger than 32kB or
requiring allocation in multiples of the system memory page size
- platform documentation should be referred to for more information
(4kB pages are common; using multiples of 4096 for the stack size is
the suggested approach in the absence of more specific information).

Returns: size

Variables Details [hide private]

_active

Value:
{139673421911936: <_MainThread(MainThread, started 139673421911936)>}

_counter

Value:
<method-wrapper 'next' of itertools.count object at 0x7f0818379998>