def _readmsg(conn): data = yield conn.readline(SENTINEL) if data is None or SENTINEL not in data: # `data` can be None because of a questionable decision in # bluelet to return None from a socket operation when it raises # an exception. I should fix this sometime. yield bluelet.end() # Socket closed. data = data[:-len(SENTINEL)] obj = _msg_deser(data) yield bluelet.end(obj)
def child(): print 'Child started.' yield bluelet.null() print 'Child resumed.' yield bluelet.null() print 'Child ending.' yield bluelet.end(42)
def fetch(cls, url): """Fetch content from an HTTP URL. This is a coroutine suitable for yielding to bluelet. """ client = cls.from_url(url) yield client._connect() yield client._request() status, headers, body = yield client._read() yield bluelet.end(body)
def scheduler(self): while self.run: if not self.pipe.poll(): yield bluelet.null() if self.run_scheduling: if len(self.engines_idle) > 0 and len(self.jobs_idle) > 0: yield bluelet.spawn(self.schedule_job()) else: recv = self.pipe.recv() BCK.__dict__[recv[0]](self, *recv[1:]) yield bluelet.end()
def schedule_job(self): unlucky = self.engines_idle.pop() self.engines_executing.append(unlucky) _,lucky = self.jobs_idle.popitem() lucky.engine_id = unlucky.id # assignment must happen before reinsertion self.jobs_executing[lucky.id] = lucky yield bluelet.call(unlucky.start_job(lucky.id)) # print('job finished: %s' % ('\n'.join(self.jobs_executing[lucky.id].output_queue))) self.jobs_finished[lucky.id] = self.jobs_executing[lucky.id] # print('job finished: %s' % ('\n'.join(self.jobs_finished[lucky.id].output_queue))) del self.jobs_executing[lucky.id] unlucky.executing_job = None self.engines_executing.remove(unlucky) self.engines_idle.append(unlucky) yield bluelet.end()
def channel(port=4915): # Create a pair of connected sockets. connections = [None, None] listener = bluelet.Listener('127.0.0.1', port) def listen(): connections[0] = yield listener.accept() # Avoiding nonlocal. listen_thread = listen() yield bluelet.spawn(listen_thread) connections[1] = yield bluelet.connect('127.0.0.1', port) yield bluelet.join(listen_thread) # Wrap sockets in Endpoints. sentinel = uuid.uuid4().bytes # Somewhat hacky... yield bluelet.end((Endpoint(connections[0], sentinel), Endpoint(connections[1], sentinel)))
def _read(self): buf = [] while True: data = yield self.conn.recv(4096) if not data: break buf.append(data) response = ''.join(buf) # Parse response. headers, body = response.split("\r\n\r\n", 1) headers = headers.split("\r\n") status = headers.pop(0) version, code, message = status.split(' ', 2) headervals = {} for header in headers: key, value = header.split(": ") headervals[key] = value yield bluelet.end((int(code), headers, body))
def func1(param): yield bluelet.sleep(param) yield bluelet.end(1)
def get(self): data = yield self.conn.readline(self.sentinel) data = data[:-len(self.sentinel)] yield bluelet.end(pickle.loads(data))