I am trying to build a GUI frontend for a very simple windows terminal application (binary available here https://github.com/00-matt/randomx-stress/releases/download/200109/randomx-stress-windows-200109.zip), and start the terminal application as a subprocess using popen and feed the output into a queue, then read the queue and put the output into the tkinter GUI. No matter what I try though I am unable to get absolutely anything from stdout or stderr from the spawned subprocess. This is a stripped down version of my code:
from tkinter import *
import subprocess
import threading
import queue
import os
from os import system
from re import sub
import sys
os.environ["PYTHONUNBUFFERED"] = "1"
def enqueue_output(p, q):
while True:
out = p.stdout.readline()
if out == '' and p.poll() is not None:
break
if out:
print(out.strip(), flush=True)
q.put_nowait(out.strip())
class Window(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.master = master
self.started = False
self.p = None
self.q = queue.Queue()
self.threads = 1
self.init_window()
def init_window(self):
self.master.title("RandomX Stress Tester")
self.pack(fill=BOTH, expand=1)
self.hashrateLabel = Label(self, text="Hashrate: ")
self.hashrateLabel.after(2000, self.refresh_hashrate)
self.startButton = Button(self, text="Start", background="green", command=self.startstop)
self.quitButton = Button(self, text="Quit", command=self.client_exit)
self.hashrateLabel.place(x=50, y=220)
self.startButton.place(x=50, y=270)
self.quitButton.place(x=220, y=270)
def startstop(self):
if not self.started:
self.p = subprocess.Popen([r"randomx-stress.exe", "-t", str(self.threads)],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
shell=True,
encoding='utf-8',
errors='replace')
self.t = threading.Thread(target=enqueue_output, args=(self.p, self.q))
self.t.daemon = True
self.t.start()
self.started = True
self.startButton.config(text="Stop", background="red")
elif self.started:
system("taskkill /im randomx-stress.exe /f")
self.p.kill()
self.t.join()
self.started = False
self.startButton.config(text="Start", background="green")
def refresh_hashrate(self):
print("checking hashrate if running")
if not self.started:
pass
elif self.started:
print("its running")
try:
line = self.q.get_nowait()
print(line)
if 'H/s' in line:
hashrate = line.split(' ')[0]
self.hashrateLabel.text = "Hashrate: {0:.2f} h/s".format(hashrate)
except:
print("error")
self.hashrateLabel.after(2000, self.refresh_hashrate)
def client_exit(self):
exit()
root = Tk()
root.geometry("400x300")
app = Window(root)
root.mainloop()
My suspicion now is that the terminal application is buffering the output and I am unable to bypass that on Windows. If someone could confirm that is the issue and if possible offer a workaround I would appreciate it!
randomx-stress.exea long running process? Because it is not run directly from the console, the program output is buffered and flushed to your script on block boundaries. On unix-like systems you can fix this with a pseudo-tty but I don't know any way to fix it on windows.flush()after write and it works. I don't remember its exact name, but there is a "is_a_tty" flag in the C libraries that control flush on write (Microsoft has published its clib implementation and you can dig it up). On unix, this is controlled by whether stdout is a tty or pipe. On windows, the console is not a stream - its a 2 dimensional grid of characters and a streaming console is emulated on top of that. But Microsoft never added a way to control this bit, as far as I am aware.endlflushes sostd::cout << hashes / difference.count() << " H/s" << endl;instead of putting\nin the stream.