Asynchronous implementation multiprocess | blinker

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/ypgsh/article/details/102729799

First, how to implement asynchronous function call, and immediately return the information to the user to perform functions without waiting for the end, similar to celery function

    By Kai multiprocess multithreaded, when the parent process exits, the child process will be terminated.

   This time when if you want to exit the parent process child process to continue, you can finally add os._exit in the parent (0) do not take the normal exit.

  Call in a multi-threaded request process in the flask, the call returns, the child process still works. In fact, it did not complete the request because the flask to terminate the parent process, just completed a request.

Two, blinker signal can achieve it

blinker function as a function of decoupling messaging, I began to think that when an entity send a message back, it will notify all registered listeners function will execute asynchronously, and later discovered that in fact is provided by a generator yield a serial synchronous execution.

def receivers_for(self, sender):
        """Iterate all live receivers listening for *sender*."""
        # TODO: test receivers_for(ANY)
        if self.receivers:
            sender_id = hashable_identity(sender)
            if sender_id in self._by_sender:
                ids = (self._by_sender[ANY_ID] |
                       self._by_sender[sender_id])
            else:
                ids = self._by_sender[ANY_ID].copy()
            for receiver_id in ids:
                receiver = self.receivers.get(receiver_id)
                if receiver is None:
                    continue
                if isinstance(receiver, WeakTypes):
                    strong = receiver()
                    if strong is None:
                        self._disconnect(receiver_id, ANY_ID)
                        continue
                    receiver = strong
                yield receiver

 

Guess you like

Origin blog.csdn.net/ypgsh/article/details/102729799