Python 'print' in a C ++ based flow model

I am developing a Python application by calling the C ++ DLL, I have placed my interaction between my DLL and Python 3.4 here . But now I need to do some streaming process using a thread-based model, and my callback function seems to queue all print , and only when my streaming has finished all information is printed.

 def callbackU(OutList, ConList, nB): for i in range(nB): out_list_item = cast(OutList[i], c_char_p).value print("{}\t{}".format(ConList[i], out_list_item)) return 0 

I tried using the following methods, but they all work the same way:

 from threading import Lock print_lock = Lock() def save_print(*args, **kwargs): with print_lock: print (*args, **kwargs) def callbackU(OutList, ConList, nB): for i in range(nB): out_list_item = cast(OutList[i], c_char_p).value save_print(out_list_item)) return 0 

and

 import sys def callbackU(OutList, ConList, nB): for i in range(nB): a = cast(OutList[i], c_char_p).value sys.stdout.write(a) sys.stdout.flush() return 0 

I would like my callback to print its message when it is called, and not when the whole process ends.

+5
source share
1 answer

I can find what the problem is, I am using a thread-based process that should remain indefinitely until it ends. In C ++, I use getchar() to wait for the process to complete, and then when I hit the enter button, the process goes to the freed part. I also tried to use sleep() 0.5 seconds in while until a certain time elapsed to check if this could help me, but it is not. Both methods worked the same in my Python application, the values ​​I needed for streaming were first queued, and if the process did not finish printing the values.

The solution was to make two functions, the first to initialize the flow-based model. And the last function to complete the process. Thus I do not need getchar() not a sleep() . This is very good for me! Thank you for your attention!

+2
source

Source: https://habr.com/ru/post/1236382/


All Articles