-1

I've got a OpenCL program with some printf() function and this program run over pyOpenCL. I want to store the stdout in a variable. My actual code to try achieve this is:

from io import StringIO # Python3 use: from io import StringIO
import sys
old_stdout = sys.stdout
sys.stdout = mystdout = StringIO()
# fpga_conv = conv2d.Matmul(H, fpga_mtx, s_mask_ref.astype(np.int32), 1, 0)
# Call to the OpenCL program via pyOpenCL
fpga_ref = conv2d.Matmul(H_ans, fpga_mtx, s_mask_ans.astype(np.int32), 1, 1, (H_h, H_w))
sys.stdout = old_stdout
string = mystdout.getvalue() # Store stdout in a var

But only catchs the python print() stdout. It's possible get all the stdout? The only way that I've found is running the script in bash with the instruction python module.py &> "ref.txt" that stores the printf() stdout in ref.txt file. I need to run the program via IDE (Spyder in this case), not in the bash terminal. Any sugesstion or trick? Thx!

Diego Ruiz
  • 187
  • 2
  • 11
  • 1
    Yes. With stderr the output is empty – Diego Ruiz Apr 16 '20 at 05:05
  • Hi @Prune I don't want redirect the output to a file. In my question I say that python module.py &> "ref.txt" work's perfectly but i need execute in the IDE, not in the bash terminal. – Diego Ruiz Apr 16 '20 at 05:17
  • Okay; I reopened. I suspect that what you'll want is a subprocess whose output you take as the process output. See Popen documentation. You can also work your way through the logic in [this thread](https://stackoverflow.com/questions/53965917/streaming-read-from-subprocess) -- I needed to monitor the output as it came through; you can use some of the other links to find a simple redirection. – Prune Apr 16 '20 at 19:40
  • Thx @Prune! I've answered my question with a solution via wurlitzer module. – Diego Ruiz Apr 17 '20 at 19:30

1 Answers1

0

Finally I've found a solution via wurlitzer module -> https://github.com/minrk/wurlitzer

Works perfectly and only needs 2 or 3 lines of code. The example for my question is (with StringIO):

out = StringIO()
with pipes(stdout=out, stderr=STDOUT):
    w_fpga_ref = conv2d.Matmul(w_H_ans, fpga_mtx, w_s_mask_ans.astype(np.int32), 1, 1, (w_H_h, w_H_w))
x = out.getvalue()
Diego Ruiz
  • 187
  • 2
  • 11