在python循环中执行time.sleep(1)时,docker卡住了

时间:2017-02-14 10:34:47

标签: python loops docker docker-compose sleep

使用docker-composepython:2.7时,只有while 1循环和time.sleep(1)分别执行时才能正常运行。

但是在一起执行时它会停滞不前。

以下是我的mac

上的泊坞窗版本和文件内容
tmp docker -v
Docker version 1.12.5, build 7392c3b
tmp cat docker-compose.yml
version: '2'
services:
    test:
        image: python:2.7
        command: [python, -c, "print 0\nwhile 1:\n\tprint 1\n\tbreak"]
tmp docker-compose up
Creating network "tmp_default" with the default driver
Creating tmp_test_1
Attaching to tmp_test_1
test_1  | 0
test_1  | 1
tmp_test_1 exited with code 0
tmp cat docker-compose.yml
version: '2'
services:
    test:
        image: python:2.7
        command: [python, -c, "print 0\nimport time\nprint time.sleep(1)"]
tmp docker-compose up
Recreating tmp_test_1
Attaching to tmp_test_1
test_1  | 0
test_1  | None
tmp_test_1 exited with code 0
tmp cat docker-compose.yml
version: '2'
services:
    test:
        image: python:2.7
        command: [python, -c, "print 0\nimport time\nwhile 1:\n\tprint time.sleep(1)"]
tmp docker-compose up
Recreating tmp_test_1
Attaching to tmp_test_1

并且在这里卡住了。

希望知道解决问题的原因和方法,谢谢。

1 个答案:

答案 0 :(得分:5)

为python添加-u标志,以便拥有无缓冲的stdout:

command: [python, -uc, "print 0\nimport time\nwhile 1:\n\tprint time.sleep(1)"]