2017-02-27 9 views
1

私はCygwinを使ってPythonコードを実行しようとしています。スクリプトはスレッドを開始し、それを処理する必要があります。しかし、それはどういうわけか機能しません。最小限の例として、ここではコード「http://www.saltycrane.com/blog/2008/09/simplistic-python-thread-example/」を使用します。 下のログにあるように、Thread.start()はメッセージなしでCygwinのPyhton対話型入力を終了します。対照的に、別のマシンではプログラムは期待どおりに実行されます。私はcygwinの問題を期待していますが、Cygwin上のPythonパッケージを再インストールしても役に立たない。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() 
... 

[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() 

作品書き換えが、第2相の出力は、スレッド間で重複ができました。

$ 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

OK、私のせいではっきり言っていませんが、cygwinの問題です!投稿したプログラムは別のマシンでも動作します。 - > Question adjusted ... – Mario

+0

私のバージョンはcygwinのpythonで動作します。あなたはそれをテストしましたか? 'import time、threading'と' threading.Thread'の欠如は実行エラーを引き起こしていました。 – matzeri

+0

これを実行しています。 '%i'のために構文エラーが発生しました。\ n "この問題を修正した後、スクリプトは出力なしで実行されます... – Mario

関連する問題