Skip to content

How to close websockets coroutines ? #754

Closed
@Insoleet

Description

Hello,

I'm running the following code which hangs :

import quamash
import asyncio
import aiohttp
import sys


class Testor:
    def __init__(self):
        self.loop = asyncio.get_event_loop()
        self.myWS = None

    async def connect(self):
        self.myWS = aiohttp.ws_connect('ws://metab.ucoin.io:9201/ws/block')
        async with self.myWS as ws:

            async for msg in ws:
                if msg.tp == aiohttp.MsgType.text:
                    print(msg.data)
                elif msg.tp == aiohttp.MsgType.closed:
                    print("closed")
                    break
                elif msg.tp == aiohttp.MsgType.error:
                    print("error")
                    break

    def disconnect(self):
        self.myWS.close()

    def run(self):
        self.loop.call_later(2, self.disconnect)
        self.loop.run_until_complete(self.connect())

test = Testor()
test.run()

What am I doing wrong and how am I supposed to close websockets sessions ?

I also tried the following, which resulted in the same behaviour :

    def disconnect(self):
        asyncio.wait_for(self.myWS.close(), timeout=2)

Thanks a lot for your great library.

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions