Created
February 19, 2019 14:34
-
-
Save Zwork101/ca4afe985ad5a03589038421c16c6223 to your computer and use it in GitHub Desktop.
Communicate with game instances.
This file contains hidden or 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 | |
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