Threading
Matt
matt.mailinglists at gmail.com
Fri Jan 24 16:33:33 EST 2020
So I would create 10 threads. And each would pop items off list like so?
def start_test():
while big_list:
list_item = big_list.pop()
print list_item, port
sleep(1)
port = 80
for i = 1 to 10
t = Thread(target=start_test)
t.start()
t.join()
Would that be thread safe?
On Fri, Jan 24, 2020 at 2:44 PM 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.
>
> ChrisA
> --
> https://mail.python.org/mailman/listinfo/python-list
More information about the Python-list
mailing list