first of all: Hello everbody, my name is Matthias and this is my first post here. I hope that this not a duplicate of an already solve question.
Currently I am struggling with running mutliple threads in NX Open Python. The reason why I want to use multiple threads is that I have some heavy jobs and I want to execute them in the background while keeping the GUI responsive and not frozen.
My setting is that I have build a new NX Ribbon and with the Block Styler some dialogs and my code is running in the event handlers of the dialogs. I work with NX10 and my OS is Windows. Please find attached a minimal example which you can simply install on Windows with the included install.cmd.
I have tried to include # nx:threaded but this did not solve the problem. Could anybody have a look at my example and tell what is missing.
ah, yes I do remember this problem. You need a space between "#nx:" and "threaded"
The online documentation does not mention this space
Clarification: The NX 10 documentation does not mention this space.
Based on the earlier forum post, the NX 11 documentation was updated to specifically mention this
Use this token as follows: •A single space between "nx:" and "threaded" is necessary. A tab space is not allowed. •The line where the token is placed must start with the Python single line comment "#". •A space or tab between "#" and "nx" is not needed. You can add that, but it is not needed.
Thanks for your replies. The void space solved my problem to get the script running inside NX. However, inside my multithreaded script I execute a statement
msg = "hello from background thread"
NXOpen.UI.GetUI().NXMessageBox.Show("", NXOpen.NXMessageBox.DialogType.Information, msg)
This leads to the following error message the system log file:
>>>> O/S ERROR: signal 11 caught in file "o:\ugnx1003\ip5\src\geomint\no\ind\jax_nxmessage_box.c" at line number 89
Python(stderr): Exception in thread Thread-1:
Python(stderr): Traceback (most recent call last):
Python(stderr): File "C:\Program Files\Siemens\NX 10.0\ugii\python\Python33.zip\threading.py", line 637, in _bootstrap_inner
Python(stderr): File "C:\Program Files\Siemens\NX 10.0\ugii\python\Python33.zip\threading.py", line 594, in run
Python(stderr): self._target(*self._args, **self._kwargs)
Python(stderr): File "C:\projects\NX\NXOpen\NX_Scripte\application\Thread\prod_cons.py", line 51, in produce
Python(stderr): XOpen.UI.GetUI().NXMessageBox.Show("",NXOpen.NXMessageBox.DialogType.Information, msg)
Python(stderr): NXOpen.NXException: Internal error: memory access violation
Escpecially the last two lines show that calling the message box is the critical operation. Do you know what is going wrong here?
P.S. my file prod_cons.py starts with
#nx: threaded (yes, this time with void between : and t)
This worked for me neither. It seems that NXOpen.Session and NXOpen.UI objects somehow have trouble with multithreaded environments.
Probably I will create a window from native python. I will post my solution as soon as I have found something.
Thanks for your reply
hmm, these are the NX Open libraries I typically import. Note, I have commented out NXOpen.UI and Utilities. I don't have any issues with multithreaded libraries like numpy or scikit-learn
Thanks for your reply. After several not successful attempts to open a NX window from a background thread I gave up to do this inside the NX Open Python environment.
Instead I shifted all the multithreading part in the extra python envorinment that ships with our application. There it was finally very easy to get things running and to program a short notification window with tkinter.
Thanks again for your help.