【发布时间】:2013-05-15 09:22:13
【问题描述】:
我认为管道连接的每个进程都是异步工作的,但事实并非如此。
a.py
#!/usr/bin/env python
import sys
import time
for line in sys.stdin:
time.sleep(2)
sys.stdout.write(line.upper())
sys.stdout.flush()
和 b.py
#!/usr/bin/env python
import sys
for line in sys.stdin:
sys.stdout.write(line.capitalize())
sys.stdout.flush()
和 test.txt
hello
world
python
以下代码以 2 秒为单位逐行显示。
$ ./a.py < test.txt
HELLO
WORLD
PYTHON
但以下代码完全显示一次。
$ ./a.py < test.txt | ./b.py
Hello
World
Python
看起来 shell pipe 正在同步工作。我怎样才能异步进行?
【问题讨论】: