Threading

Dan Sommers 2QdxY4RzWzUUiLuE at potatochowder.com
Fri Jan 24 16:15:05 EST 2020


On Sat, 25 Jan 2020 07:43:49 +1100
Chris Angelico <rosuav at gmail.com> wrote:

> 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.

Or use a thread pool:

    https://docs.python.org/3/library/concurrent.futures.html

Dan

-- 
“Atoms are not things.” – Werner Heisenberg
Dan Sommers, http://www.tombstonezero.net/dan


More information about the Python-list mailing list