This commit is contained in:
Jason Lee
2024-03-15 23:26:04 +08:00
parent 1789a28657
commit afec1a3a23
2 changed files with 51 additions and 35 deletions

32
demo.py
View File

@@ -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()