Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migration of unmigrated content due to installation of a new plugin

toc-local

Introduction

Provides an easy way to manage a pool of threads and submit and reap tasks run by those threads.

Examples

Generation of random numbers (known count):

Code Block
languagepy
linenumberstrue
import random
from threadpool import ThreadPool
 
tp = ThreadPool(5)
for i in range(100):
    tp.add(i, random.random)
results = [tp.reap() for i in range(100)]
print("\n".join(map(str, results)))

Notes:

  • pool of 5 threads available to run tasks
  • 100 tasks queued up
  • all 100 tasks are expected and reaped 

Generation of random numbers (unknown count):

Code Block
languagepy
linenumberstrue
import random
from threadpool import ThreadPool

tp = ThreadPool(5)
for i in range(100):
    tp.add(i, random.random)
tp.drain()
results = []
while not tp.is_empty():
    res = tp.reap()
    results.append(res)
print("\n".join(map(str, results)))

Notes:

  • tp.drain() may interrupt the processing of tasks so that the number of completed tasks is not known
  • tp.is_empty() checks for waiting, running, and done tasks


Panel
bgColorsnow
borderStylenone

About

Requirements: Python

License: BSD-3

Repository: https://bitbucket.org/johnmdev/threadpool

Email: expldotinfo@gmail.com

Panel
bgColorsnow
borderStylenone

Activity

Recently Updated
max10
hideHeadingtrue
themeconcise