tools.threading
— Thread-based Classes#
Classes which can be used for threading.
- class tools.threading.BoundedPoolExecutor(executor, /, max_bound=None, *args, **kwargs)[source]#
Bases:
Executor
A bounded Executor which limits prefetched Futures.
BoundedThreadPoolExecutor behaves like other executors derived from concurrent.futures.Executor but will block further items on
submit()
calls to be added to workers queue if the max_bound limit is reached.Added in version 10.0.
- Parameters:
executor (futures.Executor | str) – One of the executors found in
concurrent.futures
. The parameter may be given as class type or its name.max_bound (int | None) – the maximum number of items in the workers queue. If not given or None, the number is set to max_workers.
args (Any) – Any positional argument for the given executor
kwargs (Any) – Any keyword argument for the given executor
- Raises:
AttributeError – given executor is not found in concurrent.futures.
TypeError – given executor is not a class or not a real subclass of concurrent.futures.Executor.
ValueError – minimum max_bound is 1.
- Return type:
- class tools.threading.RLock(*args, **kwargs)[source]#
Bases:
object
Context manager which implements extended reentrant lock objects.
This RLock is implicit derived from threading.RLock but provides a locked() method like in threading.Lock and a count attribute which gives the active recursion level of locks.
Usage:
>>> lock = RLock() >>> lock.acquire() True >>> with lock: print(lock.count) # nested lock 2 >>> lock.locked() True >>> lock.release() >>> lock.locked() False
Added in version 6.2.
- property count#
Return number of acquired locks.
- class tools.threading.ThreadList(limit=128, wait_time=2.0)[source]#
Bases:
list
A simple threadpool class to limit the number of simultaneous threads.
Any threading.Thread object can be added to the pool using the
append()
method. If the maximum number of simultaneous threads has not been reached, the Thread object will be started immediately; if not, the append() call will block until the thread is able to start.Example:
pool = ThreadList(limit=10) def work(): time.sleep(1) for x in range(20): pool.append(threading.Thread(target=work))
Changed in version 10.0: the unintentional and undocumented args parameter was removed.
See also
- Parameters:
limit (int) – the number of simultaneous threads
wait_time (float) – how long to wait if active threads exceeds limit
- class tools.threading.ThreadedGenerator(group=None, target=None, name='GeneratorThread', args=(), kwargs=None, qsize=65536)[source]#
Bases:
Thread
Look-ahead generator class.
Runs a generator in a separate thread and queues the results; can be called like a regular generator.
Subclasses should override self.generator, not self.run
Important: the generator thread will stop itself if the generator’s internal queue is exhausted; but, if the calling program does not use all the generated values, it must call the generator’s stop() method to stop the background thread. Example usage:
>>> gen = ThreadedGenerator(target=range, args=(20,)) >>> try: ... data = list(gen) ... finally: ... gen.stop() >>> data [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
Added in version 3.0.
Initializer. Takes same keyword arguments as threading.Thread.
target must be a generator function (or other callable that returns an iterable object).
- Parameters:
qsize (int) – The size of the lookahead queue. The larger the qsize, the more values will be computed in advance of use (which can eat up memory and processor time).
name (str)