Threading

Cameron Simpson cs at cskk.id.au
Fri Jan 24 17:19:27 EST 2020


First come remarks, then a different suggestion about your capacity 
problem (no more than 10). Details below.

On 24Jan2020 16:03, Matt <matt.mailinglists at gmail.com> wrote:
>Created this example and it runs.
[...]
>big_list = []
>for i in range(1, 200):
>    big_list.append(i)

You can just go:

    big_list.extend(range(1,200))

>def start_test():
>    while big_list: #is this????

This tests if big_list is nonempty. In Python, most "containers" (lists, 
tuples, dicts, sets etc) are "falsey" if empty and "true" if nonempty.

>        list_item = big_list.pop() #and this thread safe????

Don't think so.

>        print list_item, port

Please use Python 3; Python 2 is end of life. So:

    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()

This starts only 10 Threads.

>print "Waiting on Threads..."
>t.join()

This waits for only the last Thread.

My suggestion for your capacity thing: use a Semaphore, which is a 
special thread safe counter which cannot go below zero.

    from threading import Semaphore

    def start_test(sem, args...):
        sem.acquire()
        ... do stuff with args ...
        sem.release()

    sem = Semaphore(10)

    threads = []
    for item in big_list:
        t = Thread(target=start_test, args=(sem, item))
        t.start()
        threads.append(t)
    ... wait for all the threads here ...

This version starts many threads, but only 10 at a time will do "work" 
because they stall until they can acquire the Semaphore. The first 10 
acquire it immediately, then the later only stall until an earlier 
Thread releases the Semaphore.

Cheers,
Cameron Simpson <cs at cskk.id.au>


More information about the Python-list mailing list