|
INTERVIEW QUESTIONS
WEB
PYTHON
DETAILS
Question: None of my threads seem to run: why?
Answer: As soon as the main thread exits, all threads are killed. Your main thread is running too quickly, giving the threads no time to do any work.
A simple fix is to add a sleep to the end of the program that's long enough for all the threads to finish:
import threading, time
def thread_task(name, n): for i in range(n): print name, i
for i in range(10): T = threading.Thread(target=thread_task, args=(str(i), i)) T.start()
time.sleep(10) # <----------------------------!
But now (on many platforms) the threads don't run in parallel, but appear to run sequentially, one at a time! The reason is that the OS thread scheduler doesn't start a new thread until the previous thread is blocked.
A simple fix is to add a tiny sleep to the start of the run function:
def thread_task(name, n): time.sleep(0.001) # <---------------------! for i in range(n): print name, i
for i in range(10): T = threading.Thread(target=thread_task, args=(str(i), i)) T.start()
time.sleep(10)
Instead of trying to guess how long a time.sleep() delay will be enough, it's better to use some kind of semaphore mechanism. One idea is to use the Queue module to create a queue object, let each thread append a token to the queue when it finishes, and let the main thread read as many tokens from the queue as there are threads.
|
|
|
Category |
Python Interview Questions & Answers -
Exam Mode /
Learning Mode
|
Rating |
(0.2) By 8342 users |
Added on |
9/23/2014 |
Views |
69876 |
Rate it! |
|
|
Question:
None of my threads seem to run: why?
Answer:
As soon as the main thread exits, all threads are killed. Your main thread is running too quickly, giving the threads no time to do any work.
A simple fix is to add a sleep to the end of the program that's long enough for all the threads to finish:
import threading, time
def thread_task(name, n): for i in range(n): print name, i
for i in range(10): T = threading.Thread(target=thread_task, args=(str(i), i)) T.start()
time.sleep(10) # <----------------------------!
But now (on many platforms) the threads don't run in parallel, but appear to run sequentially, one at a time! The reason is that the OS thread scheduler doesn't start a new thread until the previous thread is blocked.
A simple fix is to add a tiny sleep to the start of the run function:
def thread_task(name, n): time.sleep(0.001) # <---------------------! for i in range(n): print name, i
for i in range(10): T = threading.Thread(target=thread_task, args=(str(i), i)) T.start()
time.sleep(10)
Instead of trying to guess how long a time.sleep() delay will be enough, it's better to use some kind of semaphore mechanism. One idea is to use the Queue module to create a queue object, let each thread append a token to the queue when it finishes, and let the main thread read as many tokens from the queue as there are threads. Source: CoolInterview.com
If you have the better answer, then send it to us. We will display your answer after the approval.
Rules to Post Answers in CoolInterview.com:-
- There should not be any Spelling Mistakes.
- There should not be any Gramatical Errors.
- Answers must not contain any bad words.
- Answers should not be the repeat of same answer, already approved.
- Answer should be complete in itself.
|
|
Related Questions |
View Answer |
|
How do I test a Python program or component?
|
View Answer
|
|
Why don't my signal handlers work?
|
View Answer
|
|
How do I make a Python script executable on Unix?
|
View Answer
|
|
Where is the math.py (socket.py, regex.py, etc.) source file?
|
View Answer
|
|
When I edit an imported module and reimport it, the changes don't show up. Why does this happen?
|
View Answer
|
|
How do I find the current module name?
|
View Answer
|
|
How can I overload constructors (or methods) in Python?
|
View Answer
|
|
How do I create static class data and static class methods?
|
View Answer
|
|
How can I organize my code to make it easier to change the base class?
|
View Answer
|
|
How do I call a method defined in a base class from a derived class that overrides it?
|
View Answer
|
|
I want to do a complicated sort: can you do a Schwartzman Transform in Python?
|
View Answer
|
|
How do I apply a method to a sequence of objects?
|
View Answer
|
|
How do I apply a method to a sequence of objects?
|
View Answer
|
|
How do I create a multidimensional list?
|
View Answer
|
|
How do you remove duplicates from a list?
|
View Answer
|
|
How do I iterate over a sequence in reverse order?
|
View Answer
|
|
What's a negative index?
|
View Answer
|
|
How do I convert between tuples and lists?
|
View Answer
|
|
Is there a scanf() or sscanf() equivalent?
|
View Answer
|
|
Is there an equivalent to Perl's chomp() for removing trailing newlines from strings?
|
View Answer
|
Please Note: We keep on updating better answers to this site. In case you are looking for Jobs, Pls Click Here Vyoms.com - Best Freshers & Experienced Jobs Website.
View All Python Interview Questions & Answers - Exam Mode /
Learning Mode
|