3

Is it possible to reuse python thread object to avoid unnecessary creation of the thread? It could be useful in the following situation. There are many tasks which must be parallelized using thread pool which size is much less than the number of the tasks.

I know that there is multiprocessing.Pool, but it is very important to use threads not processes.

itun
  • 3,439
  • 12
  • 51
  • 75
  • 1
    possible duplicate of [Python thread pool similar to the multiprocessing Pool?](http://stackoverflow.com/questions/3033952/python-thread-pool-similar-to-the-multiprocessing-pool) – John Zwinck Dec 24 '14 at 05:35

1 Answers1

3

If you're using Python 3, the best way is definetly to use concurrent.futures.ThreadPoolExecutor.

Actually you should read the whole documentation of concurrent.futures, it's not long, and there are many great examples.

laike9m
  • 18,344
  • 20
  • 107
  • 140
  • Is there something the same for Python 2? – itun Dec 24 '14 at 07:58
  • @itun concurrent.futures has been backported to Py2, see https://pypi.python.org/pypi/futures, or you could try `ThreadPool` mentioned in http://stackoverflow.com/questions/3033952/python-thread-pool-similar-to-the-multiprocessing-pool, I haven't used it so there's not much more I coud say. – laike9m Dec 24 '14 at 08:05