我试图使用cygwin来运行我的Python代码。该脚本应该启动一个线程并对其进行处理。但它不知何故不起作用。作为一个最小的例子,我使用了这里的代码' http://www.saltycrane.com/blog/2008/09/simplistic-python-thread-example/'。
正如您在下面的日志中所看到的,Thread.start()
结束了cygwin中的Pyhton交互式输入而没有任何消息。
相反,在另一台机器上,程序按预期运行。我期待一个cygwin问题,但是在cygwin上重新安装Python包并没有帮助。
想法?
$ 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()
...
xyz@mypc~
$
答案 0 :(得分: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