faqts : Computers : Programming : Languages : Python : Common Problems : Threads

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

7 of 7 people (100%) answered Yes
Recently 2 of 2 people (100%) answered Yes

Entry

How can x threads be started at once and stopped y seconds later, processing only the successfully returned threads?

Jun 7th, 2000 05:07
unknown unknown, Gordon McMillan


Answer:

Threads have to stop themselves. You can't "kill" a thread without 
jeopardizing the integrity of the process.

For example:

Consider a situation where all the links from multiple URL's must be 
retrieved at the same time. By setting a time limit, slow sites don't 
slow down the whole process and appropriate error messages are returned 
stating which sites weren't completed in y seconds.

Solution:

You need to use a method of retrieving that recognizes time limits. In 
your case, you have 3 choices:

- a version of urllib where urlopen uses select with a timeout and 
aborts  if the timeout expires (Aahz may have done this?).
- a version of urllib that uses non-blocking sockets (then no threads 
needed).
- use separate processes instead of separate threads (because you *can* 
kill a process and properly release resources).

If none of those options are available to you - just ignore the thread 
if it's too slow.