Python打印没什么?

spe*_*rgy 1 python printing multithreading

只是按照一个教程,并据我所知完全复制它,但我的根本不打印任何东西,它应该显然从每个功能打印,但我似乎没有得到任何回报,不知道为什么?

#!/usr/bin/env python

import thread
import time
import random

def runOften(threadName, sleepTime):
    while 1 < 2:
        time.sleep(sleepTime)
        print "%s" %(threadName)

def runLessOften(threadName, sleepTime):
    while 1 < 2:
        time.sleep(sleepTime)
        print "%s" %(threadName)

def runRandomly(threadName, sleepTime):
    while 1 < 2:
        time.sleep(sleepTime)
        print "%s" %(threadName)

try:
    thread.start_new_thread(runOften, ("Often Runs", 2))
    thread.start_new_thread(runLessOften, ("Less Often Runs!", 2))
    thread.start_new_thread(runRandomly, ("Fast and random", random.random()))

except Exception, e:
    print str(e) 
Run Code Online (Sandbox Code Playgroud)

Jam*_*lls 6

把它放在代码的末尾:

while True:
    pass
Run Code Online (Sandbox Code Playgroud)

您的程序过早终止.

你的"MainThread"中没有任何内容.你产生3个线程,但在所谓的"主程序"中什么都不做.所以Python正常终止.

随着上面添加到最后我得到以下输出:

$ python foo.py
Fast and random
Fast and random
Fast and random
Often Runs
 Less Often Runs!
Fast and random
Fast and random
Fast and random
^CTraceback (most recent call last):
  File "foo.py", line 30, in <module>
    while True:
KeyboardInterrupt
Run Code Online (Sandbox Code Playgroud)