福冨諭の福冨論

RSSリーダーではこちらをどうぞ→https://feeds.feedburner.com/fuktommy

Pythonのスレッド上限

朔のアクセスがとんでもないことになっているらしい」の件で、 「マルチスレッドが楽しい」で解決できたんですけど、 Pythonのスレッドっていくつくらい作れるのかなと思ってやってみました。 たぶん環境依存なんだろうな。

#!/usr/bin/python
 from time import sleep
 from threading import Thread
 for i in range(1000):
     print i
     t = Thread(target = lambda: sleep(120))
     t.setDaemon(True)
     t.start()
 

まずFedoraCode6+Python2.4の場合。 こんな感じで子スレッドは303個まで作れました。

301
 302
 303
 Traceback (most recent call last):
   File "./threadtest.py", line 8, in ?
     t.start()
   File "/usr/lib/python2.4/threading.py", line 416, in start
     _start_new_thread(self.__bootstrap, ())
 thread.error: can't start new thread
 
 Unhandled exception in thread started by
 Error in sys.excepthook:
 
 Original exception was:
 

Cygwin+Python2.5の場合は817個までで、 Windows+Python2.5の場合は999個まで行けました。

エラーが出たときは概算ですけど、 6レス×10ノード×5スレだと思えば300個の更新通知が来たわけで、 こりゃ確かにスレッドの上限に達してもおかしくないなあ、と思いました。

Original Article: http://blog.fuktommy.com/1183037593