-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🎉 Runs GUI and Kernel on separate threads CAUTION application crashes if multiple executions are launched at the same time due to the lack of execution flow
- Loading branch information
Showing
3 changed files
with
65 additions
and
19 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# OpenCodeBlock an open-source tool for modular visual programing in python | ||
# Copyright (C) 2021 Mathïs FEDERICO <https://www.gnu.org/licenses/> | ||
|
||
""" Module to create and manage multi-threading workers """ | ||
|
||
import asyncio | ||
from PyQt5.QtCore import QObject, pyqtSignal, QRunnable | ||
|
||
|
||
class WorkerSignals(QObject): | ||
""" Defines the signals available from a running worker thread. """ | ||
stdout = pyqtSignal(str) | ||
image = pyqtSignal(str) | ||
|
||
|
||
class Worker(QRunnable): | ||
""" Worker thread """ | ||
|
||
def __init__(self, kernel, code): | ||
""" Initialize the worker object. """ | ||
super(Worker, self).__init__() | ||
|
||
self.kernel = kernel | ||
self.code = code | ||
self.signals = WorkerSignals() | ||
|
||
async def run_code(self): | ||
""" Run the code in the block """ | ||
# Execute the code | ||
self.kernel.client.execute(self.code) | ||
done = False | ||
# While the kernel sends messages | ||
while done is False: | ||
# Save kernel message and send it to the GUI | ||
output, output_type, done = self.kernel.update_output() | ||
if done is False: | ||
if output_type == 'text': | ||
self.signals.stdout.emit(output) | ||
elif output_type == 'image': | ||
self.signals.image.emit(output) | ||
|
||
def run(self): | ||
""" Execute the run_code method asynchronously. """ | ||
loop = asyncio.new_event_loop() | ||
asyncio.set_event_loop(loop) | ||
loop.run_until_complete(self.run_code()) | ||
loop.close() |