1################################################################################ 2# THIS FILE IS 100% GENERATED BY ZPROJECT; DO NOT EDIT EXCEPT EXPERIMENTALLY # 3# Read the zproject/README.md for information about making permanent changes. # 4################################################################################ 5from . import utils 6from . import destructors 7libczmq_destructors = destructors.lib 8 9class Zloop(object): 10 """ 11 event-driven reactor 12 """ 13 14 def __init__(self): 15 """ 16 Create a new zloop reactor 17 """ 18 p = utils.lib.zloop_new() 19 if p == utils.ffi.NULL: 20 raise MemoryError("Could not allocate person") 21 22 # ffi.gc returns a copy of the cdata object which will have the 23 # destructor called when the Python object is GC'd: 24 # https://cffi.readthedocs.org/en/latest/using.html#ffi-interface 25 self._p = utils.ffi.gc(p, libczmq_destructors.zloop_destroy_py) 26 27 def reader(self, sock, handler, arg): 28 """ 29 Register socket reader with the reactor. When the reader has messages, 30 the reactor will call the handler, passing the arg. Returns 0 if OK, -1 31 if there was an error. If you register the same socket more than once, 32 each instance will invoke its corresponding handler. 33 """ 34 return utils.lib.zloop_reader(self._p, sock._p, handler, arg._p) 35 36 def reader_end(self, sock): 37 """ 38 Cancel a socket reader from the reactor. If multiple readers exist for 39 same socket, cancels ALL of them. 40 """ 41 utils.lib.zloop_reader_end(self._p, sock._p) 42 43 def reader_set_tolerant(self, sock): 44 """ 45 Configure a registered reader to ignore errors. If you do not set this, 46 then readers that have errors are removed from the reactor silently. 47 """ 48 utils.lib.zloop_reader_set_tolerant(self._p, sock._p) 49 50 def poller(self, item, handler, arg): 51 """ 52 Register low-level libzmq pollitem with the reactor. When the pollitem 53 is ready, will call the handler, passing the arg. Returns 0 if OK, -1 54 if there was an error. If you register the pollitem more than once, each 55 instance will invoke its corresponding handler. A pollitem with 56 socket=NULL and fd=0 means 'poll on FD zero'. 57 """ 58 return utils.lib.zloop_poller(self._p, item._p, handler, arg._p) 59 60 def poller_end(self, item): 61 """ 62 Cancel a pollitem from the reactor, specified by socket or FD. If both 63 are specified, uses only socket. If multiple poll items exist for same 64 socket/FD, cancels ALL of them. 65 """ 66 utils.lib.zloop_poller_end(self._p, item._p) 67 68 def poller_set_tolerant(self, item): 69 """ 70 Configure a registered poller to ignore errors. If you do not set this, 71 then poller that have errors are removed from the reactor silently. 72 """ 73 utils.lib.zloop_poller_set_tolerant(self._p, item._p) 74 75 def timer(self, delay, times, handler, arg): 76 """ 77 Register a timer that expires after some delay and repeats some number of 78 times. At each expiry, will call the handler, passing the arg. To run a 79 timer forever, use 0 times. Returns a timer_id that is used to cancel the 80 timer in the future. Returns -1 if there was an error. 81 """ 82 return utils.lib.zloop_timer(self._p, delay, times, handler, arg._p) 83 84 def timer_end(self, timer_id): 85 """ 86 Cancel a specific timer identified by a specific timer_id (as returned by 87 zloop_timer). 88 """ 89 return utils.lib.zloop_timer_end(self._p, timer_id) 90 91 def ticket(self, handler, arg): 92 """ 93 Register a ticket timer. Ticket timers are very fast in the case where 94 you use a lot of timers (thousands), and frequently remove and add them. 95 The main use case is expiry timers for servers that handle many clients, 96 and which reset the expiry timer for each message received from a client. 97 Whereas normal timers perform poorly as the number of clients grows, the 98 cost of ticket timers is constant, no matter the number of clients. You 99 must set the ticket delay using zloop_set_ticket_delay before creating a 100 ticket. Returns a handle to the timer that you should use in 101 zloop_ticket_reset and zloop_ticket_delete. 102 """ 103 return utils.lib.zloop_ticket(self._p, handler, arg._p) 104 105 def ticket_reset(self, handle): 106 """ 107 Reset a ticket timer, which moves it to the end of the ticket list and 108 resets its execution time. This is a very fast operation. 109 """ 110 utils.lib.zloop_ticket_reset(self._p, handle._p) 111 112 def ticket_delete(self, handle): 113 """ 114 Delete a ticket timer. We do not actually delete the ticket here, as 115 other code may still refer to the ticket. We mark as deleted, and remove 116 later and safely. 117 """ 118 utils.lib.zloop_ticket_delete(self._p, handle._p) 119 120 def set_ticket_delay(self, ticket_delay): 121 """ 122 Set the ticket delay, which applies to all tickets. If you lower the 123 delay and there are already tickets created, the results are undefined. 124 """ 125 utils.lib.zloop_set_ticket_delay(self._p, ticket_delay) 126 127 def set_max_timers(self, max_timers): 128 """ 129 Set hard limit on number of timers allowed. Setting more than a small 130 number of timers (10-100) can have a dramatic impact on the performance 131 of the reactor. For high-volume cases, use ticket timers. If the hard 132 limit is reached, the reactor stops creating new timers and logs an 133 error. 134 """ 135 utils.lib.zloop_set_max_timers(self._p, max_timers) 136 137 def set_verbose(self, verbose): 138 """ 139 Set verbose tracing of reactor on/off. The default verbose setting is 140 off (false). 141 """ 142 utils.lib.zloop_set_verbose(self._p, verbose) 143 144 def set_nonstop(self, nonstop): 145 """ 146 By default the reactor stops if the process receives a SIGINT or SIGTERM 147 signal. This makes it impossible to shut-down message based architectures 148 like zactors. This method lets you switch off break handling. The default 149 nonstop setting is off (false). 150 """ 151 utils.lib.zloop_set_nonstop(self._p, nonstop) 152 153 def start(self): 154 """ 155 Start the reactor. Takes control of the thread and returns when the 0MQ 156 context is terminated or the process is interrupted, or any event handler 157 returns -1. Event handlers may register new sockets and timers, and 158 cancel sockets. Returns 0 if interrupted, -1 if canceled by a handler. 159 """ 160 return utils.lib.zloop_start(self._p) 161 162 def test(verbose): 163 """ 164 Self test of this class. 165 """ 166 utils.lib.zloop_test(verbose) 167 168################################################################################ 169# THIS FILE IS 100% GENERATED BY ZPROJECT; DO NOT EDIT EXCEPT EXPERIMENTALLY # 170# Read the zproject/README.md for information about making permanent changes. # 171################################################################################ 172