Showing results for 
Search instead for 
Do you mean 
Reply

NX Open Python API - multithreading

Dear All,

 

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.

 

Best regards,

 

Matthias

7 REPLIES

Re: NX Open Python API - multithreading

ah, yes I do remember this problem. Smiley Happy You need a space between "#nx:" and "threaded"

#nx: threaded

import numpy
import sklearn

The online documentation does not mention this space

 

https://community.plm.automation.siemens.com/t5/NX-Programming-Customization/multithreaded-Python-ex...

Re: NX Open Python API - multithreading

[ Edited ]

@AlexLee

 

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

 

Run threaded extension modules with Python

 

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.

 

Re: NX Open Python API - multithreading

Thanks for your replies. The void space solved my problem to get the script running inside NXSmiley Happy. 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): self.run()
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?

 

Best regards,

 

Matthias

 

P.S. my file prod_cons.py starts with

 

#nx: threaded  (yes, this time with void between : and t)

import NXOpen

Re: NX Open Python API - multithreading

@JimB thank you for clarifying that

 

@MatthiasH I have typically used self.theLw.WriteLine to print messages so that may help

 

        self.theLw = self.theSession.ListingWindow #create print window        
        self.theLw.Open()
        self.theLw.WriteLine("Hello NX World!")

Re: NX Open Python API - multithreading

Hi AlexLee,

 

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 replySmiley Happy

 

Matthias

Re: NX Open Python API - multithreading

[ Edited ]

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

import NXOpen
import NXOpen.BlockStyler
import NXOpen.Features
import NXOpen.UF
#import NXOpen.UI
import NXOpen.GeometricAnalysis
import NXOpen.Facet
#import NXOpen.Utilities
#import System

 

Re: NX Open Python API - multithreading

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.

 

Matthias