update
This commit is contained in:
32
demo.py
32
demo.py
@@ -1,16 +1,24 @@
|
||||
import signal
|
||||
import concurrent.futures
|
||||
import time
|
||||
|
||||
def handler(signo, frame):
|
||||
raise RuntimeError("Timeout")
|
||||
# Define the function you want to run with a timeout
|
||||
def my_task():
|
||||
print("Task started")
|
||||
# Simulate a long-running task
|
||||
time.sleep(5)
|
||||
print("Task completed")
|
||||
return "Task result"
|
||||
|
||||
signal.signal(signal.SIGALRM, handler)
|
||||
# Main program
|
||||
def main():
|
||||
with concurrent.futures.ThreadPoolExecutor(max_workers=1) as executor:
|
||||
future = executor.submit(my_task)
|
||||
try:
|
||||
# Wait for 2 seconds for my_task to complete
|
||||
result = future.result(timeout=2)
|
||||
print(f"Task completed with result: {result}")
|
||||
except concurrent.futures.TimeoutError:
|
||||
print("Task did not complete in time")
|
||||
|
||||
while True:
|
||||
try:
|
||||
signal.alarm(5) # seconds
|
||||
time.sleep(10)
|
||||
print("Working...")
|
||||
except Exception as e :
|
||||
print(e)
|
||||
continue
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user