Hello I can’t find a solution for the example here. I found https://stackoverflow.com/a/36666420/6089311 but there is no stream reading.
I’d like to run two subprocesses independent of python program (running in the background). The first subprocess feeds the second process through the pipe and I want to do some processing with the stdout lines by stream way.
The example bellow is blocked but I don’t know why:
JavaScript
x
18
18
1
import asyncio
2
import os
3
4
async def foo():
5
read, write = os.pipe()
6
process_1 = await asyncio.create_subprocess_exec('ls', stdout=write)
7
process_2 = await asyncio.create_subprocess_exec('wc', stdin=read, stdout=asyncio.subprocess.PIPE)
8
9
async for l in process_2.stdout:
10
# streaming process data
11
print(l)
12
13
os.close(write)
14
os.close(read)
15
16
await foo() # jupyter call
17
# async.run(foo()) # python call
18
Advertisement
Answer
Code works for me if I move close()
in places like in your link.
But probably it is not what you expect.
JavaScript
1
19
19
1
import asyncio
2
import os
3
4
async def foo():
5
read, write = os.pipe()
6
7
process_1 = await asyncio.create_subprocess_exec('ls', stdout=write)
8
os.close(write)
9
10
process_2 = await asyncio.create_subprocess_exec('wc', stdin=read, stdout=asyncio.subprocess.PIPE)
11
os.close(read)
12
13
async for line in process_2.stdout:
14
# streaming process data
15
print(line.decode())
16
17
#await foo() # jupyter call
18
asyncio.run(foo()) # python call
19
Eventually I can close read
later but I have to close write
before for
-loop.
JavaScript
1
20
20
1
import asyncio
2
import os
3
4
async def foo():
5
read, write = os.pipe()
6
7
process_1 = await asyncio.create_subprocess_exec('ls', stdout=write)
8
process_2 = await asyncio.create_subprocess_exec('wc', stdin=read, stdout=asyncio.subprocess.PIPE)
9
10
os.close(write)
11
12
async for line in process_2.stdout:
13
# streaming process data
14
print(line.decode())
15
16
os.close(read)
17
18
#await foo() # jupyter call
19
asyncio.run(foo()) # python call
20