2

I have a GCE server setup to handle some data analysis. I can communicate with it via ws using twisted. I am the only client of this server.

System is setup like this:

spawn_multiprocessing_hierarchy()
reactor.run()  # Blocks main thread
stop_everything_and_cleanup()

When I'm trying to stop the system and a client is connected, reactor will ignore (or delay indefinitely perhaps?) SIGTERM because it is handling client's connection. However, every other part of the system is fault-tolerant and reactor never handles any critical data. It exists solely for monitoring purposes. This means that I could easily SIGKILL it were it not for other multiprocess.Processes which need to dump data in memory so that they could continue where they stopped last on next launch.

Is it possible to have SIGTERM immediately (without waiting for running tasks in reactor to finish) drop any connections and stop the reactor?

Mirac7
  • 1,566
  • 4
  • 26
  • 44

1 Answers1

0

Without seeing the rest of your code, it's difficult to assume what the exact issue is. Generally, when the reactor doesn't stop, it's because a task is running in a thread or process. Twisted will try to do the "right thing" and will wait until all threads/processes are finished before exiting. You could add an event when the reactor is stopped via reactor.addSystemEventTrigger.

notorious.no
  • 4,919
  • 3
  • 20
  • 34
  • I know that my reactor doesn't stop because a task is running. Given that only one client ever connects to the server, I am okay with having reactor execute a very long task, because I only ever need one simultaneously executing on the server. I'd like to make reactor drop whatever task it's doing because it's never mission critical, and it's delaying system exit when one is required. – Mirac7 Nov 14 '16 at 20:19
  • One way around this is not spawning processes through Twisted (ie. ``reactor.spawnProcess``, ``task.deferToThread``, ``threads.callInThread``, etc), rather manage the process object manually and set ``daemon = True``. This way Twisted won't care about those threads/processes and they simply "die" when your app stops. – notorious.no Nov 14 '16 at 22:12