-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paths7_worker.py
50 lines (42 loc) · 1.42 KB
/
s7_worker.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
from PyQt5.QtCore import QObject, pyqtSignal
import asyncio
from concurrent.futures import ThreadPoolExecutor
from s7_client import run
class S7Worker(QObject):
finished = pyqtSignal(dict)
error = pyqtSignal(str)
def __init__(self, connection_params):
super().__init__()
self.connection_params = connection_params
self.executor = ThreadPoolExecutor(max_workers=1)
async def run_async(self):
"""
Asynchronously connect to the S7 server and scrape nodes.
"""
try:
memory_map = await asyncio.get_event_loop().run_in_executor(
self.executor,
run,
self.connection_params['ip']
)
self.finished.emit(memory_map)
except Exception as e:
self.error.emit(f"Failed to connect or scrape nodes: {e}")
def run_worker(self):
"""
Wrapper to execute the asynchronous run in the asyncio event loop.
Initializes and runs the event loop within this thread.
"""
try:
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(self.run_async())
except Exception as e:
self.error.emit(f"Runtime error in worker: {e}")
finally:
loop.close()
def run(self):
"""
Public method to start the worker.
"""
self.run_worker()