简体   繁体   中英

Python subprocesses - passing multiple commands in a loop

I am trying to run a c++ implementation of a model ( sent2Vec ) from a python p program using the subprocess module. The c++ program runs a loop like the following:

while(raw_input):
    print(perform_action(input))

This is because it loads a huge model into memory and then uses it as and when a query is passed to it. So, the c++ output looks like this:

Pre-computing vectors... done.
Query sentence?

query
0.603723 1 a1
0.306778 2 a2
0.262201 4 a3
0.198674 0 a4
0.131687 3 a5
0.121209 5 a6
0 6

Query sentence?

I would like to run the program and then when required send an input and read the output. I know the number of lines of generated per query so it's fine if I have to read the entire output every time.

I tried the following:

process = Popen(cmd, stdin=PIPE, stdout=PIPE) process.stdin.write(query) process.stdout.readlines()

However, this seems to get stuck in a loop. I looked at multiple stackoverflow threads and most of them suggest what I tried. I would really appreciate any help.

Thanks in advance!

There was a deadlock situation. You need to exactly count the number of lines written to stdout and only then write to stdin .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM