Issue
I never used multithreading before so I searched an easy example and found this approach from 8 years ago that opens 10 threads and joins them later:
(I liked it because I also needed to save the results of my thread functions)
How to get the return value from a thread in python?
def foo(bar, result, index):
print 'hello {0}'.format(bar)
result[index] = "foo"
from threading import Thread
threads = [None] * 10
results = [None] * 10
for i in range(len(threads)):
threads[i] = Thread(target=foo, args=('world!', results, i))
threads[i].start()
# do some other stuff
for i in range(len(threads)):
threads[i].join()
print " ".join(results)
I managed to implemented this into my code (that I know to work correctly without multi-threading) and the implementation works: No error and it seems everything finishes and does what it should.
Just afterwards iPython hardly responds anymore (super slow) so I can't continue working. (I can hardly type and new code I enter in iPython runs super slow)
Any ideas what might cause the problem?
According to the ResourceManager the CPUs are still processing something. But why? The script has finished! At least I am 99% sure it did. I am truly thankful for any tips!
Solution
What is their in "# do some other stuff"? You can add towards the end is_alive() to check if it is the thread or some other stuffs that are causing this lag.
Minimally edited code (w.r.t. yours) is as below.
def foo(bar, result, index):
print 'hello {0}'.format(bar)
result[index] = "foo"
from threading import Thread
threads = [None] * 10
results = [None] * 10
for i in range(len(threads)):
threads[i] = Thread(target=foo, args=('world!', results, i))
threads[i].start()
# do some other stuff
for i in range(len(threads)):
threads[i].join()
print " ".join(results)
for i in range(len(threads)):
print(threads[i].is_alive())
If towards the end it prints 10 times False then none of your threads are alive! They have done their job and they are out of the way, otherwise they are still working.
Answered By - Amit
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.