Getting python terminal output
31 ビュー (過去 30 日間)
古いコメントを表示
I am calling a python function from Matlab that usually to take a couple of hours to finish a run. If the function is run as a proper Python script from Windows terminal, it prints out the operations being done etc. i.e. displays a log:
Cleaning Sig_Traces 1
Loaded Matrix_Weights
Iteration 1 in progress.
Checking for fit ...
So, at any point of time, I have a clue about the progress. Now, how to make this displayable in Matlab's own terminal (or wherever - I don't care) when calling the function from Matlab? I cannot be really staring at a "Busy" footer in Matlab for hours.
2 件のコメント
Delprat Sebastien
2024 年 3 月 2 日
You cannot easily get the print output.
However, if you use the logging module, you can use this in your python code to send repports to matlab. This snipet may have to be reworked but you should have the overall idea
import logging
HANDLER = logging.StreamHandler()
FORMATTER = logging.Formatter('[%(levelname)s] %(filename)s - %(lineno)d - %(message)s')
HANDLER.setFormatter(FORMATTER)
LOGGER = logging.getLogger('YOUR MODULE NAME') # <= modify here
LOGGER.addHandler(HANDLER)
LOGGER.setLevel(logging.WARNING) # pick whatever level suits
% Then in your code (pick one of these)
LOGGER.info('my message')
LOGGER.warning('my message')
LOGGER.error('my message')
回答 (2 件)
Walter Roberson
2023 年 4 月 19 日
In the special case where you are running Python from Windows, then you could use the .NET assembly System.Diagnostics.Process to run the python executable while being able to access its standard output.
2 件のコメント
Walter Roberson
2023 年 4 月 19 日
Mathworks does not provide much information on the topic because Mathwork's only concern is being able to interface with .NET assemblies in general; what you can do with those assemblies is up to Microsoft and the people who write the assemblies.
The following posts may be useful
Al Danial
2023 年 4 月 20 日
The issue is that matlab buffers Python's print statements and only flushes this buffer--meaning print to the Python text in the matlab command window--when enough text has been added. All you need to do is print a bunch of useless text from Python to fill the buffer before printing your progress message. The best "useless" text is that which has no negative impact on the output you want to see. To that end I use a large number of copies of a space followed by a backspace, so a net zero of cursor movement, and in the end your text appears as it would normally.
Here's sample Python code that does this:
import time
def compute(n_iter):
for i in range(int(n_iter)):
time.sleep(0.4)
x = i*i / 7.3 - 0.2
print(' \b'*4000, end='') # <- this is the trick
print(f'{i:3d} {x: 10.6f}')
The second to last line fills the print buffer with no effect on the position of the "real" text which is to be printed (on the last line in this example). Tested on Linux with 2022b.
1 件のコメント
Walter Roberson
2023 年 4 月 20 日
However this is not relevant to the behaviour of .NET System.Diagnostics.Process.
.NET -- 4K buffer size, prone to deadlock
But .NET notifications can be delayed until newline is received
参考
カテゴリ
Help Center および File Exchange で Python Package Integration についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!