Skip to content

Commit

Permalink
Fix: standard out is opened several times (#89)
Browse files Browse the repository at this point in the history
* Fix: standard out is opened several times

* Optimize imports
  • Loading branch information
sverben authored Dec 1, 2023
1 parent 5067559 commit 5045459
Show file tree
Hide file tree
Showing 10 changed files with 25 additions and 24 deletions.
8 changes: 7 additions & 1 deletion index.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@
import importlib.util
import random
import signal
import sys
import time
from glob import glob
from json import loads
from os import path
from typing import Optional, Any, List

import aiofiles

import mqtt
from states.base import BaseState
from states.socket import Socket
Expand All @@ -24,6 +27,7 @@ def __init__(self, cmd_args):
self.states = {}
self.games = self.get_games()
self.socket = Socket(self, int(cmd_args.port))
self.stdout = None

def import_module(self, loc: str) -> (Optional[Any], str):
name = path.basename(loc).replace('.mod.py', '', -1)
Expand Down Expand Up @@ -63,7 +67,7 @@ def read_states(self) -> None:
# Re-create any missing states (killed threads)
if name not in self.states:
try:
state = module.State()
state = module.State(self.stdout)
except Exception:
continue
state.name = name
Expand Down Expand Up @@ -155,6 +159,8 @@ def stop(self):

async def start(self) -> None:
async with asyncio.TaskGroup() as tg:
self.stdout = await aiofiles.open(sys.stdout.fileno(), 'wb', 0)

tg.create_task(self.state_loop())
tg.create_task(self.socket.start())

Expand Down
9 changes: 2 additions & 7 deletions states/base.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import sys
from abc import abstractmethod
from os import environ
from shutil import which
from typing import List

import aiofiles
import httpx
from PIL import Image

Expand All @@ -18,15 +16,15 @@ class BaseState:
delay = 10
index = 0

def __init__(self):
def __init__(self, stdout):
super().__init__()
self.killed = False
self.on_pi = which("rpi-update")
self.player_class = None
self.game_meta = None
self.font_path = "./static/fonts/NotoMono-Regular.ttf"
self.client = httpx.AsyncClient()
self.stdout = None
self.stdout = stdout

def kill(self) -> None:
self.killed = True
Expand Down Expand Up @@ -54,9 +52,6 @@ async def output_image(self, pil_image: Image) -> None:
await self.output_frame(pil_image.tobytes())

async def output_frame(self, frame: bytes) -> None:
if not self.stdout:
self.stdout = await aiofiles.open(sys.stdout.fileno(), 'wb', 0)

await self.stdout.write(frame)

@staticmethod
Expand Down
4 changes: 2 additions & 2 deletions states/games/pacman.mod.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,8 @@ class State(BaseState):
source = ""
direction = [1, 0]

def __init__(self):
super().__init__()
def __init__(self, stdout):
super().__init__(stdout)
self.player_class = Player
self.game_meta = "static/game_meta/pacman.json"

Expand Down
4 changes: 2 additions & 2 deletions states/games/pong2.mod.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,8 @@ class State(BaseState):
winw = 96
winh = 32

def __init__(self):
super().__init__()
def __init__(self, stdout):
super().__init__(stdout)
self.player_class = Player
self.game_meta = "static/game_meta/pong2.json"

Expand Down
4 changes: 2 additions & 2 deletions states/games/snake.mod.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,8 @@ def export(self):


class State(BaseState):
def __init__(self):
super().__init__()
def __init__(self, stdout):
super().__init__(stdout)
self.name = "snake"
self.game_meta = "static/game_meta/snake.json"
self.index = 8
Expand Down
4 changes: 2 additions & 2 deletions states/games/splash.mod.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,8 @@ class State(BaseState):
calls = 0
game = None

def __init__(self):
super().__init__()
def __init__(self, stdout):
super().__init__(stdout)
self.player_class = Connection
self.game_meta = "static/game_meta/splash.json"

Expand Down
4 changes: 2 additions & 2 deletions states/games/tetris.mod.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,8 +367,8 @@ class State(BaseState):
game = None
source = ""

def __init__(self):
super().__init__()
def __init__(self, stdout):
super().__init__(stdout)
self.player_class = Player
self.game_meta = "static/game_meta/tetris.json"

Expand Down
4 changes: 2 additions & 2 deletions states/idle/place.mod.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ class State(BaseState):
image = Image.new("RGB", (W, H), "black")
time = 0

def __init__(self):
super().__init__()
def __init__(self, stdout):
super().__init__(stdout)
self.sio.on("board", self.board)
self.sio.on("color", self.color)
self.sio.connect("https://place.djoamersfoort.nl")
Expand Down
4 changes: 2 additions & 2 deletions states/idle/pong.mod.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ class State(BaseState):
color = bytes([255, 255, 255])
frame_delay = 1 / 30

def __init__(self):
super().__init__()
def __init__(self, stdout):
super().__init__(stdout)
self.state = None

# check function
Expand Down
4 changes: 2 additions & 2 deletions states/idle/temp.mod.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ class State(BaseState):
index = 0
delay = 12

def __init__(self):
super().__init__()
def __init__(self, stdout):
super().__init__(stdout)
self.state = None

# module check function
Expand Down

0 comments on commit 5045459

Please sign in to comment.