Skip to content

Instantly share code, notes, and snippets.

@no1xsyzy
Last active October 14, 2020 02:22
Show Gist options
  • Save no1xsyzy/03fb6d436dc3e87a1d2d12505d9234fe to your computer and use it in GitHub Desktop.
Save no1xsyzy/03fb6d436dc3e87a1d2d12505d9234fe to your computer and use it in GitHub Desktop.
from multiprocessing import Process, Queue
from queue import Empty
from PyQt5.QtWidgets import *
from PyQt5.QtCore import QTimer
def calc(q):
q.put(sum(i**i for i in range(10000)))
def reline_helper(s):
import re
return '\n'.join(filter(None, re.split(r"(.{300})",s)))
if __name__ == '__main__':
app = QApplication([])
window = QWidget()
layout = QVBoxLayout()
btnCalc = QPushButton('Do Calc')
lblCalc = QLabel('idle')
is_calculating = [False]
q = Queue()
@btnCalc.clicked.connect
def on_button_clicked():
if is_calculating[0]:
return
is_calculating[0] = True
lblCalc.setText("generating process...")
p = Process(target=calc, args=(q,))
lblCalc.setText("calculating...")
p.start()
timer = QTimer(window)
@timer.timeout.connect
def timers():
try:
print("polls")
result = q.get(block=False)
print(result)
lblCalc.setText(reline_helper(str(result)))
p.join()
timer.stop()
is_calculating[0] = False
except Empty:
pass
timer.start(1000)
layout.addWidget(btnCalc)
layout.addWidget(lblCalc)
window.setLayout(layout)
window.show()
app.exec_()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment