def UDP_recv(self): self.check_closed() ec = core.get_ec() if len(self.read_queue) == 0: uv_callback.push(ec.uv__udp_recv, self) status = uv.udp_recv_start(self.udp, _udp_alloc_callback_, _udp_read_callback_once_) if status < 0: uv_callback.drop(ec.uv__udp_recv, self.udp) raise uv_callback.to_error(status) if len(self.read_queue) == 0: if self.read_greenlet is not None: raise unwind(LError(u"async collision")) self.read_greenlet = ec.current core.switch([ec.eventloop]) array, nread, status, addr, flags = self.read_queue.pop(0) if nread < 0: raise uv_callback.to_error(nread) if status < 0: raise uv_callback.to_error(status) if array is None: array = Uint8Slice(lltype.nullptr(rffi.UCHARP.TO), 0, None) elif array.length != nread: array = array.subslice(nread) res = Exnihilo() res.setattr(u"data", array) res.setattr(u"addr", addr) res.setattr(u"flags", Integer(rffi.r_long(flags))) return res
def Stream_read(self): self.check_closed() ec = core.get_ec() if len(self.read_queue) == 0: uv_callback.push(ec.uv__read, self) status = uv.read_start(self.stream, _alloc_callback_, _read_callback_once_) if status < 0: uv_callback.drop(ec.uv__read, self.stream) raise uv_callback.to_error(status) if len(self.read_queue) == 0: if self.read_greenlet is not None: raise unwind(LError(u"async collision")) self.read_greenlet = ec.current core.switch([ec.eventloop]) array, nread, status = self.read_queue.pop(0) if nread < 0: raise uv_callback.to_error(nread) if status < 0: raise uv_callback.to_error(status) if array is None: return Uint8Slice(lltype.nullptr(rffi.UCHARP.TO), 0, None) if array.length == nread: return array return array.subslice(nread)
def wait(self, status=0): if status < 0: pop_handle(getattr(self.ec, uv_name), self.handle) raise to_error(status) elif self.pending: self.greenlet = self.ec.current core.switch([self.ec.eventloop]) # TODO: prepare some mechanic, so that # we can be interrupted. return self.data
def Stream_accept(self): self.check_closed() if self.listen_count > 0: if self.accept_greenlet is not None: raise unwind(LError(u"async collision")) ec = core.get_ec() self.accept_greenlet = ec.current core.switch([ec.eventloop]) check(self.listen_status) self.listen_count -= 1 client = self.new_stream() check(uv.accept(self.stream, client.stream)) return client
def Watch_wait(self): if len(self.data) > 0: return self.data.pop(0) elif self.status < 0: raise uv_callback.to_error(self.status) else: if self.greenlet is not None: raise unwind(LError(u"fs.watch retrieval collision")) ec = core.get_ec() self.greenlet = ec.current return core.switch([ec.eventloop])
def sleep_greenlet(duration): ec = core.get_ec() if ec.current == ec.eventloop: raise space.OldError(u"bad context for greenlet sleep") assert ec.current.is_exhausted() == False uv_sleeper = uv.malloc_bytes(uv.timer_ptr, uv.handle_size(uv.TIMER)) ec.uv_sleepers[rffi.cast_ptr_to_adr(uv_sleeper)] = ec.current uv.timer_init(ec.uv_loop, uv_sleeper) uv.timer_start(uv_sleeper, wakeup_sleeper, int(duration.number * 1000), 0) return core.switch([ec.eventloop])
def typed(ruletypes): """ Apply rules based on the expression type inputs: ruletypes -- a dict mapping {Type: rule} >>> from sympy.strategies import rm_id, typed >>> from sympy import Add, Mul >>> rm_zeros = rm_id(lambda x: x==0) >>> rm_ones = rm_id(lambda x: x==1) >>> remove_idents = typed({Add: rm_zeros, Mul: rm_ones}) """ return switch(type, ruletypes)
def _fs_event_cb_(handle, filename, events, status): ec = core.get_ec() try: self = uv_callback.peek(ec.uv__fs_event, handle) status = rffi.r_long(status) if self.status == 0 and status < 0: uv_callback.drop(ec.uv__fs_event, handle) self.status = status return obj = Exnihilo() obj.setattr(u"path", from_cstring(rffi.charp2str(filename))) if rffi.r_long(events) == 1: obj.setattr(u"type", String(u"rename")) else: obj.setattr(u"type", String(u"change")) if self.greenlet is None: self.data.append(obj) else: greenlet, self.greenlet = self.greenlet, None core.switch([greenlet, obj]) except Unwinder as unwinder: core.root_unwind(ec, unwinder)
def exit(obj): ec = core.get_ec() ec.exit_status = 0 if obj is None else int(obj.value) uv.stop(ec.uv_loop) ec.enqueue(ec.current) # Trick to ensure we get Discard -exception here return core.switch([ec.eventloop]) # Once they are created.