Personal tools
You are here: Home Forum Other... (new forum, problems, etc.) print statements in TUI are delayed until the end of script (Salome 9.3)

print statements in TUI are delayed until the end of script (Salome 9.3)

Up to Other... (new forum, problems, etc.)

print statements in TUI are delayed until the end of script (Salome 9.3)

Posted by Guillaume Florent at December 09. 2019

If you try running this simplified script, it should illustrate the matter:

from time import sleep

print("Hello")
sleep(10)
print("Goodbye")

 

Everything gets displayed at once in the Python console, while one might expect to have 'Hello' displayed 10 seconds before 'Goodbye'.

For scripts that run in seconds, no problem. But for a script that takes minutes, it would be nice to have the print statements appear in 'real time' to track progress.

Is there a workaround or another way I am not aware of?

Re: print statements in TUI are delayed until the end of script (Salome 9.3)

Posted by Vadim SANDLER at December 09. 2019
Unfortunately, the way the embedded Python console is currently implemented does not allow this :(
 
If you need this to be able to print something to the embedded console in order to show the script's progress, you can use a workaround that consists in periodical call of Qt's processEvents() function.
 
This can be done either via direct PyQt calls:
 
from PyQt5.Qt import QApplication
QApplication.processEvents()
 
... or by using SalomePyQt API:
 
from SalomePyQt import SalomePyQt
SalomePyQt.processEvents()
 

Re: print statements in TUI are delayed until the end of script (Salome 9.3)

Posted by Guillaume Florent at December 09. 2019

It works perfectly! Thanks a lot for the tip. Now I can monitor long running scripts in the Python console. Great!

Re: print statements in TUI are delayed until the end of script (Salome 9.3)

Posted by Guillaume Florent at December 10. 2019

While we are at Python TUI "funny" things: the __file__ symbol does not seem to be defined. I had a workaround but it is not really reliable:

import inspect
__file__ = inspect.getfile(inspect.currentframe())

Any better idea?

 

Re: print statements in TUI are delayed until the end of script (Salome 9.3)

Posted by Vadim SANDLER at December 10. 2019

Yes, because with "Load Script" operation the srcipt is exec'ed in the console. In this case __file__ is not defined. If you want it to work, you must import your script. For this, you'll need to extend PYTHONPATH environment variable before running SALOME, to add a path where your script is located.

Quick reply
URLs will be automatically hyperlinked. Basic HTML tags are OK.
Powered by Ploneboard
History
Activate by Guillaume Florent on Dec 09, 2019 04:11 AM
Document Actions