简体   繁体   中英

Terminating a thread on completion in Python

Is there any way to have a thread terminate itself once it is finished its target function?

I have an application that requires running a wait for connection function every 2min. This function takes around 5 seconds to run.

import time
import threading

count = 0 

def fivesecondwait():
    time.sleep(5)
    print("Finished side function")

t = threading.Thread(target = twentysecondwait)

while True:
  if count == 10:
    count = 0
    t.start()

  print("Running Main Application")
  count += 1
  time.sleep(1)

If I try and join the thread the loop stops running, but if I do not include a join it results in the error "Threads can only be started once". I need the function to be called every 10 seconds and to not block the execution of the loop. So is there any way to have the thread terminate itself so it can be used again?

This is for a measurement device that needs to continue measuring every 50ms while repeatedly waiting for 3-4 seconds for a connection from an MQTT host.

Your question and your code don't quite seem to match up, but this much is true; You said, "it results in the error "Threads can only be started once", and your code appears to create a single thread instance, which it then it attempts to start() more than one time.

The error message is self explanatory. Python will not allow you to call t.start() more than one time on the same thread instance, t . The reason why is, a thread isn't just an object. It's an object that represents an execution of your code (ie, a trip through your code.)

You can't take the same trip more than one time. Even if you go to the same beach that you visited last year, even if you follow the same route, and stop to rest in all the same places, it's still a different trip. Threads obey the same model.

If you want to run twentysecondwait() multiple times "in the background," there's nothing wrong with that, but you'll have to create and start a new thread instance for each execution.


Is there a practical limit to how many thread instances can be created?

There probably is no practical limit to how many can be sequentially created and destroyed,* but each running thread occupies significant memory and uses other resources as well. So, yes, There is a limit** to how many threads can be "alive" at the same time.

Your example thread function takes 20 seconds to execute. If the program starts a new one every ten seconds, then the number of live threads will increase by one every 20 seconds—180 per hour. The program certainly will not be able to sustain that for 90 days.


* A better performing alternative to contually creating and destroying threads is to use a thread pool such as Python's concurrent.futures.ThreadPoolExecutor .

** The limit is going to depend on what version of Python you are running, on what OS you are running, on how much memory your computer has, etc. The limit probably is not well defined in any case.

A thread will automatically terminate itself when it finished running.

But:

a) you can't start the same thread twice and

b) you can't re-start a thread that terminated.

Simple solution: always start a new thread.

while True:
  if count == 10:
    count = 0
    t = threading.Thread(target = fivesecondwait)
    t.start()
  ...

[...] so it can be used again?

No. You could however keep a single thread running and use synchronization objects to tell the thread when to do something and when not.

This will require more knowledge but be less resource intense.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM