T O P I C R E V I E W |
i.reuter |
Posted - 03/08/2022 : 06:01:17 AM Dear all,
I'm experiencing issues using Python's threading library in the context of Origin's python environment.
The use of the library results in a frozen/crashed Origin app.
Can someone confirm this issue?
Best, Ingo
Steps to reproduce:
1. Create a single python file 'test.py' with content as follows:
import threading
import time
class Worker (threading.Thread):
def __init__(self):
print("Init threading")
super().__init__()
def run(self) -> None:
time.sleep(10)
print("This is done by thread... ")
if __name__ == "__main__":
print("Starting... ")
worker = Worker()
worker.start()
while worker.is_alive():
print("worker living")
time.sleep(0.5)
2. Run Python file in Origin
Open Origin's 'Command Window' via menu (Window --> Command Window), paste the command and hit enter:
run -pyf "C:\path\to\your\test.py";
System: OriginPro 2022 (64-bit) SR1 9.9.0.225 (Academic) on Microsoft Windows 10 (21H2) |
6 L A T E S T R E P L I E S (Newest First) |
i.reuter |
Posted - 04/07/2022 : 03:29:09 AM Hi minimax,
I was able to come up with a working example, combining GUI and an external worker thread.
You might find the code here: https://bitbucket.org/reuterscience/multithreadingtest/src/master/
I will continue to elaborate the example and try to identify possible issues as described in my original post.
Best Ingo |
minimax |
Posted - 04/01/2022 : 06:03:23 AM Hi i.reuter,
Supposing Origin is not freezing, what kind of actions would you prepare to do during those calculation moments?
Change options on the wxpython GUI? or what else?
If yes, do you have a simple wxpython GUI to show what you would do? |
i.reuter |
Posted - 03/29/2022 : 06:24:24 AM quote: Originally posted by cpyang
I think the problem is with the print command, as Origin channel that to display in Origin's window, which is not thread safe.
Can you avoid print and maybe write to a file instead?
CP
Hi CP,
sorry for my late response. I've tried to inspect the issue further. You seem to be right, that the print command is shown delayed in Origin. In the case of output to a file, the file output is created just in time.
You can reproduce the test with this code as given at the end of my post.
I've tried the test code with and without the two print commands. The result is always the same: The file is created correctly and the time difference of the timestamps inside the file are about 25 seconds as expected. Unfortunately, Origin freezes while the python script is running (and the print-output is delayed).
This renders a problem for my 'real world application'. In fact, I try to use threading in combination with a GUI rendered by wxpython for an Origin-App: In order to keep the GUI reactive, a second thread is started for some heavy and time-consuming calculations. All of my attempts to realize this concept, work well as stand-alone-code. As soon as the code is executed within the Origin context, origin freezes or crashes completely.
Maybe, you have some insight for me, how to handle the Origin runtime context correctly.
Best, Ingo
Test code
import threading
from datetime import datetime
from time import sleep
import originpro as op
import pathlib
filepath = pathlib.Path(pathlib.Path.home(), "Desktop/main.txt")
main_file = open(filepath, 'w')
#critical section to global data
def write(tt, name):
global v1, v2
v1.append(name)
v2.append(tt)
#must not do print or anything related to Origin inside the thread function
def thread_task(lock, name):
for _ in range(5):
sleep(5)
now = datetime.now()
tt = now.strftime("%H:%M:%S.%f")[:-3]
lock.acquire()
write(tt, name)
lock.release()
#global variables to be used inside thread task
v1=[]
v2=[]
#we need to have criticlal section for any access to shared data
lock = threading.Lock()
t1 = threading.Thread(target=thread_task, args=(lock,'a'))
t2 = threading.Thread(target=thread_task, args=(lock,'b'))
t1.start()
t2.start()
now1 = datetime.now()
main_file.write(f'{now1.strftime("%H:%M:%S.%f")[:-3]}: Waiting\n')
print("Waiting...")
t1.join()
t2.join()
now1 = datetime.now()
main_file.write(f'{now1.strftime("%H:%M:%S.%f")[:-3]}: Waiting done...\n')
print("Waiting done...")
#threads done, we can put data to a worksheet
wks=op.new_sheet()
#need to set col(2) as time format before putting data into it
wks.cols = 2
wks.as_time(1, 10)
wks.from_list(0, v1, 'Name')
wks.from_list(1, v2, 'Time')
#has to use LabTalk for setting column width
wks.lt_exec("wks.col2.width=9")
main_file.close()
|
cpyang |
Posted - 03/18/2022 : 5:41:44 PM I think the problem is with the print command, as Origin channel that to display in Origin's window, which is not thread safe.
Can you avoid print and maybe write to a file instead?
CP
|
i.reuter |
Posted - 03/17/2022 : 05:09:13 AM Hi Cpyang,
your script does not result in a crash of Origin. But Origin freezes while the script is running. For further investigation, I've expanded your code a slightly.
Test Script (modifications)
# ... unchanged ...
#must not do print or anything related to Origin inside the thread function
def thread_task(lock, name):
for _ in range(5):
sleep(5) # Change: Wait for 5 seconds
now = datetime.now()
tt = now.strftime("%H:%M:%S.%f")[:-3]
lock.acquire()
write(tt, name)
lock.release()
# ... unchanged ...
t1 = threading.Thread(target=thread_task, args=(lock,'a'))
t2 = threading.Thread(target=thread_task, args=(lock,'b'))
t1.start()
t2.start()
print("Waiting...") # Simple output (as soon as threads start)
t1.join()
t2.join()
print("Waiting done...") # Simple output (as soon as both threads finished)
# ... unchanged ...
As you can see, I've made two slightly changes: (1.) The sleep time is increased from 0.5 to 5 seconds and (2.) I've added some print statements.
Result:
As soon as you run the script by "run -pyf "path/to/script.py" Origin freezes until the script is finished. Both print statements appear at the same time in command window (as soon as Origin is responding again).
I hope, this feedback provides valuable information for you.
Best, Ingo
|
cpyang |
Posted - 03/08/2022 : 12:32:34 PM While we look into that, maybe you can try this example to see if it works?
https://github.com/originlab/Python-Samples/blob/main/7.%20Miscellaneous/Multi%20Threading.py
CP
|
|
|