tools.threading — Thread-based Classes#

Classes which can be used for threading.

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.

locked()[source]#

Return true if the lock is acquired.

class tools.threading.ThreadList(limit=128, wait_time=2, *args)[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.

>>> pool = ThreadList(limit=10)
>>> def work():
...     time.sleep(1)
...
>>> for x in range(20):
...     pool.append(threading.Thread(target=work))
...
Parameters:
  • limit (int) – the number of simultaneous threads

  • wait_time (float) – how long to wait if active threads exceeds limit

active_count()[source]#

Return the number of alive threads and delete all non-alive ones.

append(thd)[source]#

Add a thread to the pool and start it.

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)

run()[source]#

Run the generator and store the results on the queue.

Return type:

None

stop()[source]#

Stop the background thread.

Return type:

None