I have a python child process which creates a dictionary and output the dictionary using JSON. process.stdout appears to be empty. Data from the child process is not being captured.
json_str = data # dictionary
sys.stdout.write(json_str)
sys.stdout.write(‘\n’)
The python child process is invoked by python parent process. See below code. I went old school and put in display statemenst to track the logic execution.
<>
import json
import subprocess
file_path = “/content/Data_Structure.ipynb”
def main():
command = [‘python’,file_path]
Start the child process
process= subprocess.Popen(command, env=env,stdout=subprocess.PIPE, stderr=subprocess.PIPE, encoding=‘utf-8’, text=True)
Read and parse JSON data from the child process
for line in process.stdout:
try:
print('Print made it to A')
json_data = json.loads(line)
print('Received data:', json_data)
except json.JSONDecodeError:
print('Invalid JSON: ', line)
print('Made it to B')
process.wait() # Wait for the child process to finish
if name== “main”:
main()
print('Made it to C')
<>
Output:
Made it to C