Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

319
Visualizações
'Cannot run the event loop while another loop is running') RuntimeError websockets?
import asyncio
    import json
    import websockets
    from mongodb import *


    class WebSocketRequest:
        def __init__(self, websocket):
            self.websocket = websocket

        async def login(self):
            data = await self.websocket.recv()
            j = json.loads(data)
            for i in j:
                if i == 'email':
                    email = j[i]
                if i == "pass":
                    password = j[i]
            user = users.find_one({"email":email})
            if user == None:
                msg = 400
            else:
                msg = 200
            await websocket.send(str(msg))

        async def register(self):
            data = await self.websocket.recv()
            j = json.loads(data)
            print(j)

        async def run(self):

            loop = asyncio.new_event_loop()
            asyncio.set_event_loop(loop)
            asyncio.ensure_future(self.login())
            asyncio.ensure_future(self.register())
            loop.run_forever()

    class WebsocketServer:
        def __init__(self, localhost,port):
            self.localhost = localhost
            self.port = port


        async def hello(self, websocket, path):
            req = WebSocketRequest(websocket)
            await req.run()

        def run(self):
            print("opening")
            loop = asyncio.new_event_loop()
            asyncio.set_event_loop(loop)

            start_server = websockets.serve(self.hello, self.localhost, self.port)

            asyncio.get_event_loop().run_until_complete(start_server)
            asyncio.get_event_loop().run_forever()
    if __name__=='__main__':
        localhost, port = '127.0.0.1', 5678
        web = WebsocketServer(localhost, port)
        web.run()

I'm trying to build a chat-app server using WebSockets. I have 3 methods - login, register and chat. I check if a user login or not and redirect him to register in a front end. I am trying to run 3 methods in one script using classes. I got an error message because there 2 loops run.
What is the problem in my code?

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

It might not be possible. Guido van Rossum stated before that he didn't want recursive event loops And it seems like you're creating a nested event loops

over 4 years ago · Santiago Trujillo Relatório

0

Before your code just write those lines

!pip install nest_asyncio
import nest_asyncio
nest_asyncio.apply()
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda