0

I am quite new to Python.
My problem:
I am starting a bunch of threads in parallel each one trying to create a session with one of a number of foreign hosts (to perform a few activities in those sessions later). Some of these hosts may be in an awkward state in which case the session creation fails eventually, however, that takes about 60 secs. If successful, the session is created immediately. Hence I want to terminate the respective session creation threads after a reasonable time (a few secs). However, I learned that the only way to stop a thread is to communicate an event status to the thread to observe - which makes no sense if it is stuck in an action (here: to establish the session). I use join() with a timeout - that speeds up the execution for all sessions successfully created; however, main() won't exit of course until all the temporarily stuck threads have returned. Is there really no way to cut this short? Thanks for any useful advice.

Uwe

Random Davis
  • 6,662
  • 4
  • 14
  • 24
Uwe
  • 1
  • 1
  • Subclass `threading.Thread` and build in a self-destruct timer. Refactor using asyncio and use its timeout *facilities*. Does [Is there any way to kill a Thread?](https://stackoverflow.com/questions/323972/is-there-any-way-to-kill-a-thread) answer your question? – wwii Aug 16 '22 at 16:16
  • The link you provided either refers to the event-flag way (which appears not viable to me as laid out above) or to using multiprocessing instead of multithreading. However, I suppose that starting separate processes I canot just do that for the session creation (or could the session objects somehow be conveyed back to the mothership?). A third way (also mentioned) would be a hard suicide of the main process but as that script is to run frequently and automated I'd not chose that route. Could you provide some clue what you mean by "self-destruct timer" ? – Uwe Aug 16 '22 at 16:28
  • Ah, i suppose you mean something like shown in https://www.section.io/engineering-education/how-to-perform-threading-timer-in-python/#instance-2. I.e., the thread is defined as a timer thread, which can be then stopped by cancel(), correct? – Uwe Aug 16 '22 at 16:36
  • For asyncio stuff I found e.g. https://www.programcreek.com/python/example/81580/asyncio.TimeoutError -- suppose that is related.. Thx so far, will check that out ... – Uwe Aug 16 '22 at 16:39
  • As for the threading.Timer -- cancel() way: Does not look promising as I read " cancel() Stop the timer, and cancel the execution of the timer’s action. This will only work if the timer is still in its waiting stage. " - In its waiting stage the thread does not do anything, i need to cancel it having tried to establish a session in vain for more than say 4 secs ... Hence, the asyncio route remains to be checked out ... – Uwe Aug 16 '22 at 16:48
  • asyncio.wait_for(aw, timeout) appears a reasonable choice. thx again. – Uwe Aug 16 '22 at 16:59
  • 2
    Please provide enough code so others can better understand or reproduce the problem. – Community Aug 16 '22 at 17:02

0 Answers0