Threading

Chris Angelico rosuav at gmail.com
Fri Jan 24 15:43:49 EST 2020


On Sat, Jan 25, 2020 at 7:35 AM Matt <matt.mailinglists at gmail.com> wrote:
>
> I am using this example for threading in Python:
>
> from threading import Thread
>
> def start_test( address, port ):
>     print address, port
>     sleep(1)
>
> for line in big_list:
>     t = Thread(target=start_test, args=(line, 80))
>     t.start()
>
> But say big_list has thousands of items and I only want to have a
> maximum of 10 threads open.  How do work my way through the big_list
> with only 10 threads for example?

First off, it is high time you move to Python 3, as the older versions
of Python have reached end-of-life.

The best way is to create your ten threads, and have each one request
"jobs" (for whatever definition of job you have) from a queue. Once
the queue is exhausted, the threads terminate cleanly, and then you
can join() each thread to wait for the entire queue to be completed.

ChrisA


More information about the Python-list mailing list