Skip to content

Instantly share code, notes, and snippets.

@nurettin
Created August 17, 2019 05:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nurettin/f024a53f49d1e5eb1a381a0ccc996eb3 to your computer and use it in GitHub Desktop.
Save nurettin/f024a53f49d1e5eb1a381a0ccc996eb3 to your computer and use it in GitHub Desktop.
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