make client.run() block and run from sync
This commit is contained in:
parent
1d6916932b
commit
1b72f94043
2 changed files with 11 additions and 16 deletions
|
@ -1,6 +1,5 @@
|
|||
import sys
|
||||
import json
|
||||
import asyncio
|
||||
import logging
|
||||
|
||||
from .mc.proto.play.clientbound import PacketChat
|
||||
|
@ -29,9 +28,6 @@ if __name__ == "__main__":
|
|||
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
|
||||
# TODO rework how this is started! Maybe implement client context manager?
|
||||
loop = asyncio.get_event_loop()
|
||||
|
||||
client = Client(host, port, username=username, password=pwd)
|
||||
|
||||
@client.on_packet(PacketChat, ConnectionState.PLAY)
|
||||
|
@ -39,13 +35,7 @@ if __name__ == "__main__":
|
|||
msg = parse_chat(json.loads(packet.message), color=color)
|
||||
print(f"[{packet.position}] {msg}")
|
||||
|
||||
loop.run_until_complete(client.start())
|
||||
|
||||
try:
|
||||
loop.run_until_complete(idle())
|
||||
except KeyboardInterrupt:
|
||||
logging.info("Received SIGINT, stopping...")
|
||||
loop.run_until_complete(client.stop())
|
||||
client.run() # will block and start asyncio event loop
|
||||
|
||||
logging.info("Terminated")
|
||||
|
||||
|
|
|
@ -120,18 +120,23 @@ class Client:
|
|||
if restart:
|
||||
await self.start()
|
||||
|
||||
async def run(self):
|
||||
await self.start()
|
||||
def run(self):
|
||||
loop = asyncio.get_event_loop()
|
||||
|
||||
try:
|
||||
loop.run_until_complete(self.start())
|
||||
|
||||
async def idle():
|
||||
while self._processing: # TODO don't busywait even if it doesn't matter much
|
||||
await asyncio.sleep(self.options["poll-timeout"])
|
||||
|
||||
try:
|
||||
loop.run_forever(idle())
|
||||
except KeyboardInterrupt:
|
||||
self._logger.info("Received SIGINT, stopping...")
|
||||
else:
|
||||
self._logger.warning("Client terminating...")
|
||||
|
||||
await self.stop()
|
||||
loop.run_until_complete(self.stop())
|
||||
|
||||
async def start(self):
|
||||
self._processing = True
|
||||
|
@ -243,7 +248,7 @@ class Client:
|
|||
elif isinstance(packet, PacketKickDisconnect):
|
||||
self._logger.error("Kicked while in game")
|
||||
break
|
||||
for packet_type in (Packet, packet.__class__): # check both callbacks for base class and instance class
|
||||
for packet_type in (Packet, type(packet)): # check both callbacks for base class and instance class
|
||||
if packet_type in self._packet_callbacks:
|
||||
for cb in self._packet_callbacks[packet_type]:
|
||||
try: # TODO run in executor to not block
|
||||
|
|
Loading…
Reference in a new issue