2017-02-27 130 views
1

我正尝试使用cygwin来运行我的Python代码。该脚本应该启动一个线程并对其进行处理。但它以某种方式不起作用。作为一个最小的例子,我使用这里的代码'http://www.saltycrane.com/blog/2008/09/simplistic-python-thread-example/'。 正如你可以在下面的日志中看到的,Thread.start()在cygwin中结束Pyhton交互式输入,没有任何消息。 相反,在另一台机器上,程序按预期运行。我期待cygwin问题,但在cygwin上重新安装Python软件包并没有帮助。Python线程无法在cygwin上运行

想法?

$ python 
Python 2.7.12 (default, Oct 10 2016, 12:56:26) 
[GCC 5.4.0] on cygwin 
Type "help", "copyright", "credits" or "license" for more information. 
>>> import time 

def myfunc(i): 
    print "sleeping 5 sec from thread %d" % i 
    time.sleep(5) 
    print "finished sleeping from thread %d" % i 

for i in range(10): 
    t = Thread(target=myfunc, args=(i,)) 
    t.start() 
>>> from threading import Thread 
>>> 
>>> def myfunc(i): 
...  print "sleeping 5 sec from thread %d" % i 
...  time.sleep(5) 
...  print "finished sleeping from thread %d" % i 
... 
>>> for i in range(10): 
...  t = Thread(target=myfunc, args=(i,)) 
...  t.start() 
... 

[email protected]~ 
$ 

回答

0

改写这样
cat prova-python.py

#!/usr/byn/python 
import time,threading 

def myfunc(i): 
    print "sleeping 5 sec from thread %d" % i 
    time.sleep(5) 
    print "finished sleeping from thread %d" % i 

for i in range(10): 
    t = threading.Thread(target=myfunc, args=(i,)) 
    t.start() 

作品,但第二阶段的输出可以线程之间的重叠。

$ python prova-python.py 
sleeping 5 sec from thread 0 
sleeping 5 sec from thread 1 
sleeping 5 sec from thread 2 
sleeping 5 sec from thread 3 
sleeping 5 sec from thread 4 
sleeping 5 sec from thread 5 
sleeping 5 sec from thread 6 
sleeping 5 sec from thread 7 
sleeping 5 sec from thread 8 
sleeping 5 sec from thread 9 
finished sleeping from thread 0 
finished sleeping from thread 2 
finished sleeping from thread 1 
finished sleeping from thread 3 
finished sleeping from thread 4 
finished sleeping from thread 5 
finished sleeping from thread 6 
finished sleeping from thread 9finished sleeping from thread 8finished sleeping from thread 7 
+0

好吧,我的错,我没有说清楚,但它是一个cygwin问题!我发布的程序也适用于另一台机器。 - >调整问题... – Mario

+0

我的版本在cygwin python上运行。你测试过了吗?缺少“导入时间,线程”和“threading.Thread”导致执行错误。 – matzeri

+0

运行它我得到了一个语法错误,因为'%i'\ n''。解决这个问题后,脚本运行时没有任何输出... – Mario