Python Forum
Is there a way to not terminate a running thread in python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Is there a way to not terminate a running thread in python
#1
Hi All,
I have a use case where i am executing my script for a set duration. I wanted to know if there is a way to not terminate a running thread and keep on running for the set duration ex. 10 seconds?
My script is running fine but it creates a new thread as soon as the it receives the response for the previous http request. Any pointers will be helpful. Thanks!!
Reply
#2
There are a couple ways, both of which use the time library. First you'll need to import time. The first method is time.sleep(seconds), which is very simple, but sometimes you may need to do something else to run code while waiting. Here's some example code of the other method.
import time

timeStamp = time.time() #Records a number measured in epoch time
waitTime = 10

while time.time() - timeStamp < waitTime:
    #Run code here
Hope this helps
Reply
#3
Thanks i a doing something similar but instead using time.process_time() and then running the threads through a loop. Is there a way to get the average of all the requests. I am able to get the response time per execution as below. But is there a way to sum all of these and get the average?

Response:
Thread 0
Thread 1
request: 1 Status: 200OK ResponseTime: 0.003963258999999941
request: 0 Status: 200OK ResponseTime: 0.005142219999999975
Thread 0
Thread 1
request: 0 Status: 200OK ResponseTime: 0.004329180999999904
request: 1 Status: 200OK ResponseTime: 0.004248067999999994
Reply
#4
You can save the response times in a list, then find the average
responseTimesList = [1.2, 0.7, 0.9, 1.4] #These would be appended to the list somewhere in your code

average = sum(responseTimesList)/len(responseTimesList)
print("The average of the list is %s" %average)
If you're having trouble with this, post your code.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  The game should now run and terminate once the board is full, but still can’t identif rango 0 1,429 Jul-22-2021, 03:24 AM
Last Post: rango
  Pyinstaller keeps thread running pythonuser_0101 2 3,067 Mar-06-2020, 02:09 PM
Last Post: pythonuser_0101
  Error SQLite objects created in a thread can only be used in that same thread. binhduonggttn 3 15,389 Jan-31-2020, 11:08 AM
Last Post: DeaD_EyE
  Terminate a process when hotkey is pressed 66Gramms 0 2,210 Dec-24-2019, 06:41 PM
Last Post: 66Gramms
  How to terminate the caller fuction through callee? 100k 2 2,101 Nov-27-2019, 06:49 PM
Last Post: jefsummers
  How to terminate a list of inputs with a set value? SOS Manning 2 2,142 Sep-19-2019, 01:54 AM
Last Post: scidam
  Python Thread stops execution neethuvp 1 3,355 Feb-18-2019, 06:36 PM
Last Post: micseydel
  Debugging inside a thread with Python 2.3.4 zerajera 0 1,675 Nov-21-2018, 01:43 PM
Last Post: zerajera
  Is thread running? MuntyScruntfundle 0 2,051 Oct-19-2018, 06:10 PM
Last Post: MuntyScruntfundle
  how to terminate all the processes in C py2exe 0 2,050 Aug-02-2018, 04:31 AM
Last Post: py2exe

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020