Threading

Matt matt.mailinglists at gmail.com
Fri Jan 24 17:03:50 EST 2020


Created this example and it runs.

import time
import threading

big_list = []

for i in range(1, 200):
    big_list.append(i)

def start_test():
    while big_list: #is this????
        list_item = big_list.pop() #and this thread safe????
        print list_item, port
        time.sleep(1)

print "Creating Threads..."

port = 80
for i in range(1, 10):
    t = threading.Thread(target=start_test)
    t.start()

print "Waiting on Threads..."

t.join()

print "Finished..."

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