[Tutor] Calling the same thread multiple times

Vagelis vagp21 at gmail.com
Wed Apr 25 06:27:53 EDT 2018


Hello,

Im creating a progress bar for applications that can keep track of a
download in progress. The progress bar will be on a separate thread and
will communicate with the main thread using delegates.
Ive made the download and the progress bar part, all that remains is the
connection between the two of them.

For this purpose i tried to simplify the problem, but i cant seem to
make it right.

Here's what i got so far...

import threading

def test():
    print(threading.current_thread())

for i in range(5):

    print(threading.current_thread())

    t1 = threading.Thread(target = test)

    t1.start()
    t1.join()


This gives me the output:

<_MainThread(MainThread, started 139983023449408)>
<Thread(Thread-1, started 139982885717760)>
<_MainThread(MainThread, started 139983023449408)>
<Thread(Thread-2, started 139982877325056)>
<_MainThread(MainThread, started 139983023449408)>
<Thread(Thread-3, started 139982868932352)>
<_MainThread(MainThread, started 139983023449408)>
<Thread(Thread-4, started 139982860539648)>
<_MainThread(MainThread, started 139983023449408)>
<Thread(Thread-5, started 139982647850752)>

What i need to do is to call the same thread (Thread-1) multiple times,
and the call (of the test function) must be IN the for loop.

Ive also tried something like that:

import threading
import queue

def test():
    print(threading.current_thread())
    i = q.get()
    print(i)

q = queue.Queue()
t1 = threading.Thread(target = test)
t1.start()

for i in range(5):
    print(threading.current_thread())
    q.put(i)


t1.join()

The result im getting is :

<Thread(Thread-1, started 140383045297920)>
<_MainThread(MainThread, started 140383183029568)>
<_MainThread(MainThread, started 140383183029568)>
0
<_MainThread(MainThread, started 140383183029568)>
<_MainThread(MainThread, started 140383183029568)>
<_MainThread(MainThread, started 140383183029568)>

Any ideas on how to solve this?



More information about the Tutor mailing list