Created
August 17, 2019 05:17
-
-
Save nurettin/f024a53f49d1e5eb1a381a0ccc996eb3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncio | |
class Prompt: | |
def __init__(self, loop=None): | |
self.loop = loop or asyncio.get_event_loop() | |
self.q = asyncio.Queue(loop=self.loop) | |
self.loop.add_reader(sys.stdin, self.got_input) | |
def got_input(self): | |
asyncio.ensure_future(self.q.put(sys.stdin.readline()), loop=self.loop) | |
async def __call__(self, msg, end='\n', flush=False): | |
print(msg, end=end, flush=flush) | |
return (await self.q.get()).rstrip('\n') | |
prompt = Prompt() | |
async_input = functools.partial(prompt, end='', flush=True) | |
async def start_cmd(): | |
while True: | |
command = await async_input("") | |
if command == "JOIN": | |
print("joining") | |
await client.send(b"JOIN") | |
elif command == "LEAVE": | |
print("leaving") | |
await client.send(b"LEAVE") | |
elif command.startswith("MOVE"): | |
print("moving") | |
await client.send(command.encode()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment