Open leclem opened 1 month ago
The most robust way to catch an exit event is to use the signal library since there are more ways to quit than just ctrl+c.
Here's a small idea of what the code would look like:
import signal
import sys
import time
def cancel_requests(p: str):
print(f'Cancelling request to generate video with prompt "{p}"')
time.sleep(4)
sys.exit(0)
def generate_video(prompt: str):
signal.signal(signal.SIGINT, lambda _, __: cancel_requests(prompt))
while True:
print('Generating video...')
time.sleep(1)
generate_video('forest with tall trees')
It looks like the gateway classes are the right place to implement this. This code can also be abstracted with a decorator for brevity.
I'd like to work on this issue and would appreciate it if you could assign it to me
Hi Prithiv, thanks to propose your help here, yes I am assigning you to this issue,
Please note that using signal sounds good though we also need to inform the gateway that ongoing async calls need to be cleanly interrupted too, which means possibly calling a callback to inform our providers or own internal platform can stop generating music or video in some remote infrastructure.
To start with you can go ahead and propose a generic solution that leverage the signal lib, btw this probably will be extended on ffmpeg wrappers which does basic error handling for singint for now.
Using a decorator sounds interesting though I wonder if defining a single global handler is not simpler to catch and cleanup a sigint, knowing that this has to happen using the single main python thread. This would look like https://docs.python.org/3/library/signal.html#handlers-and-exceptions,
Consider using the similar solution as https://github.com/Vikit-ai/sdk/issues/130 i.e. CQRS + Command patterns
That would be interesting to send a cancel signal to replicate / Haipe/ etc. once I break the running code by ctrl+c for example