I'm writing a simple module in my Python app to communicate with the server. I use gevent and zeromq socket. This module will run in a thread.
Here is a demo
import threading
import gevent
import zmq.green as zmq
class SocketTest(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)
        self.context = zmq.Context()
        self.socket = None
        self.running = False
        self.sock_greenlet = None
    def run(self):
        self.socket = self.context.socket(zmq.DEALER)
        self.socket.setsockopt(zmq.RCVTIMEO, 1000)
        self.socket.connect('tcp://127.0.0.1:9999')
        self.running = True
        self.sock_greenlet = gevent.spawn(self.process)
        print('Starting')
        self.sock_greenlet.join()
    def process(self):
        while self.running:
            print('Wait for data')
            data = self.socket.recv()
            # do something
            print(data)
            gevent.sleep(0.5)
    def stop(self):
        print('Stop app')
        self.running = False
        # I want to ask all greenlets to exit, or kill them
        # gevent.wait(timeout=0)
        gevent.kill(self.sock_greenlet)
        print('End of stop')
def test():
    try:
        app = SocketTest()
        app.start()
        app.join()
    except KeyboardInterrupt:
        app.stop()
        print('Exit')
if __name__ == '__main__':
    test()
When I press Ctrl + C, my app doesn't exit. I understand that my thread is running an event loop. But I don't know how to stop greenlet process properly or kill it.
In main thread, I will call app.stop, is it safe if I access some variables of gevent loop ?
( I want to send a goodbye message to server when my app exits)