Skip to content

Instantly share code, notes, and snippets.

@Zwork101
Created February 19, 2019 14:34
Show Gist options
  • Save Zwork101/ca4afe985ad5a03589038421c16c6223 to your computer and use it in GitHub Desktop.
Save Zwork101/ca4afe985ad5a03589038421c16c6223 to your computer and use it in GitHub Desktop.
Communicate with game instances.
import asyncio
from os import path
from pathlib import Path
import time
import select
from subprocess import Popen
class GamePipe(Popen):
def __init__(self, lang: str, name: str):
self.file_path = path.join(Path(__file__).parents[1], "games", lang, name)
self.name = name.split('.')[0]
super().__init__([lang, self.file_path])
async def recv(self, timeout: int=None):
start_time = time.time()
while timeout is None or start_time + timeout > time.time():
if not select.select([self.stdout], [], [], 0)[0]:
await asyncio.sleep(0)
continue
return self.stdout.read()
def send(self, msg: str):
self.stdin.write(msg)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment