'Flush pending messages.
This method safely handles all pending incoming and/or outgoing messages,
bypassing the inner loop, passing them to the registered callbacks.
A limit can be specified, to prevent blocking under high load.
flush will return the first time ANY of these conditions are met:
* No more events matching the flag are pending.
* the total number of events handled reaches the limit.
Note that if ``flag|POLLIN != 0``, recv events will be flushed even if no callback
is registered, unlike normal IOLoop operation. This allows flush to be
used to remove *and ignore* incoming messages.
Parameters
flag : int, default=POLLIN|POLLOUT
0MQ poll flags.
If flag|POLLIN,  recv events will be flushed.
If flag|POLLOUT, send events will be flushed.
Both flags can be set at once, which is the default.
limit : None or int, optional
The maximum number of messages to send or receive.
Both send and recv count against this limit.
Returns
int : count of events handled (both send and recv)'

def flush(self, flag=(zmq.POLLIN | zmq.POLLOUT), limit=None):

    self._check_closed()
    already_flushed = self._flushed
    self._flushed = False
    count = 0
    def update_flag():
        "Update    the    poll    flag,    to    prevent    registering    POLLOUT    events\n                                                if    we    don't    have    pending    sends."
        return ((flag & zmq.POLLIN) | (self.sending() and (flag & zmq.POLLOUT)))
    flag = update_flag()
    if (not flag):
        return 0
    self.poller.register(self.socket, flag)
    events = self.poller.poll(0)
    while (events and ((not limit) or (count < limit))):
        (s, event) = events[0]
        if (event & zmq.POLLIN):
            self._handle_recv()
            count += 1
            if (self.socket is None):
                break
        if ((event & zmq.POLLOUT) and self.sending()):
            self._handle_send()
            count += 1
            if (self.socket is None):
                break
        flag = update_flag()
        if flag:
            self.poller.register(self.socket, flag)
            events = self.poller.poll(0)
        else:
            events = []
    if count:
        self._flushed = True
        if (not already_flushed):
            self.io_loop.add_callback(self._finish_flush)
    elif already_flushed:
        self._flushed = True
    self._rebuild_io_state()
    return count
