aioreactive.producer.AsyncStream

Here are the examples of the python api aioreactive.producer.AsyncStream taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

2 Examples 7

Example 1

Project: aioreactive Source File: autocomplete.py
Function: websocket_handler
async def websocket_handler(request):
    print("WebSocket opened")

    stream = AsyncStream()

    # Line break before binary operator is more readable. Disable W503
    xs = (stream
          | op.map(lambda x: x["term"])
          | op.filter(lambda text: len(text) > 2)
          | op.debounce(0.75)
          | op.distinct_until_changed()
          | op.map(search_wikipedia)
          | op.switch_latest()
          )

    ws = web.WebSocketResponse()
    await ws.prepare(request)

    async def asend(value):
        ws.send_str(value)

    async def athrow(ex):
        print(ex)

    await start(xs, FuncSink(asend, athrow))

    async for msg in ws:
        if msg.type == aiohttp.WSMsgType.TEXT:
            obj = json.loads(msg.data)
            await stream.asend(obj)

        elif msg.type == aiohttp.WSMsgType.ERROR:
            print('ws connection closed with exception %s' % ws.exception())

    print('websocket connection closed')
    return ws

Example 2

Project: aioreactive Source File: timeflies.py
Function: main
async def main(loop):
    root = Tk()
    root.title("aioreactive rocks")

    mousemoves = AsyncStream()

    frame = Frame(root, width=600, height=600)

    async def move(event):
        await mousemoves.asend(event)

    def call_move(event):
        asyncio.ensure_future(move(event))
    frame.bind("<Motion>", call_move)

    text = 'TIME FLIES LIKE AN ARROW'
    labels = [Label(frame, text=c) for c in text]

    async def handle_label(i, label):
        label.config(dict(borderwidth=0, padx=0, pady=0))

        async def asend(ev):
            label.place(x=ev.x + i * 12 + 15, y=ev.y)

        xs = mousemoves | op.delay(i / 10.0)
        await start(xs, FuncSink(asend))

    for i, label in enumerate(labels):
        await handle_label(i, label)

    frame.pack()

    # A simple combined event loop
    while True:
        root.update()
        await asyncio.sleep(0.01)