async def put_input_queue(self, request: web.Request): breq = HttpRequest() breq.headers = request.headers breq.cookies = request.cookies breq.url = request.url breq.path = request.path if breq.path != self.route_path: return web.Response(text="error path") breq.method = request.method breq.payload = await request.text() breq.query = request.query ori = Bdata(breq, 0) bdata = Bdata(breq, ori) #send result to q await self.output_q.put(bdata) fut = self._loop.create_future() self.waiters[bdata] = fut r = await asyncio.wait_for(fut, self.timeout) try: return web.json_response(r) except: raise
async def __call__(self, data): ori = Bdata.make_Bdata_zori(data) await self.start_q.put(Bdata(data, ori)) r = await self.output_q.get_by(ori) self.output_q.clean(ori) if isinstance(r.data, list): for i, v in enumerate(r.data): if isinstance(v, Exception): r.data[i] = None return r.data
async def fa(): ori1 = Bdata.make_Bdata_zori(1) ori2 = Bdata.make_Bdata_zori(2) q = ConditionalQueue() for i in range(100): await q.put(Bdata(4, ori2)) await q.put(Bdata(3, ori1)) o1 = asyncio.ensure_future(q.get_by(ori1)) result = await asyncio.gather(o1) assert result[0].data == 3
async def route_in_joinmerge(self, bdata): # if bdata.is_BotControl(): # await super().route_in(bdata) # # else: data = Bdata(bdata.data, bdata) data.count = 0 await super().route_in(data) r = await self.databoard.wait_ori(bdata) await self.merge_node.put_result(r) self.databoard.drop_ori(bdata)
async def route_in(self, bdata): await self.ori_list.put(bdata) new_bdata = Bdata(bdata.data, bdata) for q in self.start_q: await q.put(new_bdata)
def run(self, data=0): start_q = DataQueue() end_q = DataQueue(maxsize=0) if isinstance(data, (list, range)): self.args.insert(0, data) bdata = Bdata.make_Bdata_zori(0) else: bdata = Bdata.make_Bdata_zori(data) self._make(start_q, end_q) self._start() self.bm.loop.run_until_complete(self._true_run(bdata)) result = self.get_result() self.bm.remove_by_pipe(self) return result
async def route_in(self, bdata): if self.merge_node is None: if bdata.ori.ori == 0: new_data = Bdata(bdata.data, bdata) await super().route_in(new_data) else: await super().route_in(bdata) else: await self.route_in_joinmerge(bdata)
async def route_out(self): o = await self.ori_list.get() result = [] for q in self.output_q: try: r = await q.get_by(o) result.append(r.data) # task=self.loop.create_task(q.get_by(o)) # tasks.append(task) except Exception as e: logging.exception("excd") # r=await asyncio.gather(*tasks,get_loop()) return Bdata(result, o)
async def response_stream(self, request: web.Request): breq = HttpRequest() breq.headers = request.headers breq.cookies = request.cookies breq.url = request.url breq.path = request.path if breq.path != self.route_path: return web.Response(text="error path") breq.method = request.method breq.payload = await request.text() breq.query = request.query bdata = Bdata(breq, queue.DataQueue()) resp = web.StreamResponse(status=200, reason='OK', headers={'Content-Type': 'text/html'}) await resp.prepare(request) await self.output_q.put(bdata) while True: try: r: Bdata = await asyncio.wait_for(bdata.ori.get(), 10) # if r.is_BotControl(): # break import json json = json.dumps(r.data) await resp.write(bytes(json, encoding='utf-8')) except Exception as e: print(e) break await resp.write_eof(b'\nend\n') #await resp.write(b"heelo") #await asyncio.sleep(1) return resp
async def route_in(self, bdata): import copy self.databoard.set_ack(bdata) cp_bdata = Bdata(bdata.data, ori=0) await self.output_q.put(cp_bdata)
async def write(self, data): await self.start_q.put(Bdata.make_Bdata_zori(data))
def start(self): self.output_q.set_q(SinkQueue()) self.start_q.put_nowait(Bdata.make_Bdata_zori(0))
async def put_result(self, result): await self.output_q.put(Bdata.make_Bdata_zori(result))