Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't do gracefull shutdown of apllication #293

Closed
vokson opened this issue May 11, 2024 · 1 comment
Closed

Can't do gracefull shutdown of apllication #293

vokson opened this issue May 11, 2024 · 1 comment

Comments

@vokson
Copy link

vokson commented May 11, 2024

Granian 1.3.1

My goal is to make program handling http request and send them to Kafka. I use https://github.com/confluentinc/confluent-kafka-python library, which is syncronious. Thus, I have to implement infinitive loop flushing kafka queue in separate thread for each granian's worker. The loop runs while flag is_cancelled = False.
When I stop program (Ctrl-C, for example),

def shutdown_app():
   is_cancelled = True

if __name__ == "__main__":
    try:
        app.serve()
    except KeyboardInterrupt:
        pass
    finally:
        shutdown_app()

I expect granian will stop and somehow move to shutdown_app() function to give me possibility to set is_cancelled = True and stop infinite thread. Unfortunately, granian never stops, because infinite thread is included in granian worker process.

    def _stop_workers(self):
        for proc in self.procs:
            proc.terminate()

        for proc in self.procs:
            proc.join()  <<- HERE

        self.procs.clear()

If changed to proc.join(1)', after 1 sec execution moves ahead and comes in shutdown_app` function, then I can manage other stuff.

Possible solution:

  1. Add callback function to class Granian, which will called before stop of worker
  2. Add timeout property X to class Granian, which will be used as proc.join(X)
@gi0baro
Copy link
Member

gi0baro commented May 13, 2024

@vokson is that a WSGI application? For ASGI you can rely on the lifespan events to perform actions on shutdown.

Anyways, 1.4 will probably review signal handling and eventually add callbacks. In general, you can always customise what happens on the workers writing your own spawn method: for instance you can copy the code of _spawn_wsgi_worker (https://github.com/emmett-framework/granian/blob/master/granian/server.py#L284-L319) and add any code you want after the serve call (blocking til shutdown).

@emmett-framework emmett-framework locked and limited conversation to collaborators May 13, 2024
@gi0baro gi0baro converted this issue into discussion #295 May 13, 2024

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants