Python什么也不打印?
只是按照一个教程,据我所知,复制了它,但我的不会打印出任何东西,它显然应该不断地从每个函数打印出来,但我似乎没有得到任何东西回来,不知道为什么Python什么也不打印?,python,multithreading,printing,Python,Multithreading,Printing,只是按照一个教程,据我所知,复制了它,但我的不会打印出任何东西,它显然应该不断地从每个函数打印出来,但我似乎没有得到任何东西回来,不知道为什么 #!/usr/bin/env python import thread import time import random def runOften(threadName, sleepTime): while 1 < 2: time.sleep(sleepTime) print "%s" %(thread
#!/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)
#/usr/bin/env python
导入线程
导入时间
随机输入
def runOften(线程名称,休眠时间):
而1<2:
时间。睡眠(睡眠时间)
打印“%s%”(线程名称)
def RunleSSofte(线程名称,睡眠时间):
而1<2:
时间。睡眠(睡眠时间)
打印“%s%”(线程名称)
def随机运行(线程名称,睡眠时间):
而1<2:
时间。睡眠(睡眠时间)
打印“%s%”(线程名称)
尝试:
线程。开始新线程(runOften,(“经常运行”,2))
线程。启动新线程(runlessofte,(“较少运行!”,2))
thread.start_new_线程(随机运行,(“快速随机”,random.random())
除例外情况外,e:
打印str(e)
将此内容放在代码末尾:
while True:
pass
您的程序提前终止
您的“主线程”中没有任何内容。您生成了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
$python foo.py
快速随机
快速随机
快速随机
经常跑
少跑步!
快速随机
快速随机
快速随机
^CTraceback(最近一次通话最后一次):
文件“foo.py”,第30行,在
尽管如此:
键盘中断
将此内容放在代码末尾:
while True:
pass
您的程序提前终止
您的“主线程”中没有任何内容。您生成了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
$python foo.py
快速随机
快速随机
快速随机
经常跑
少跑步!
快速随机
快速随机
快速随机
^CTraceback(最近一次通话最后一次):
文件“foo.py”,第30行,在
尽管如此:
键盘中断
更好的解决方案是使用模块,它为线程提供了类——一个面向对象的接口
这样,我们就可以轻松地运行三个线程(等待它们完成)
调整您的示例:
import threading
import time
import random
keep_running = True
def runOften(threadName, sleepTime):
while keep_running:
time.sleep(sleepTime)
print "%s" %(threadName)
def runLessOften(threadName, sleepTime):
while keep_running:
time.sleep(sleepTime)
print "%s" %(threadName)
def runRandomly(threadName, sleepTime):
while keep_running:
time.sleep(sleepTime)
print "%s" %(threadName)
def main():
global keep_running
# Create the threads
threads = []
threads.append(threading.Thread(target=runOften, args=("Often Runs", 2)))
threads.append(threading.Thread(target=runLessOften, args=("Less Often Runs!", 2)))
threads.append(threading.Thread(target=runRandomly, args=("Fast and random", random.random())))
# Start the threads
for t in threads:
t.start()
# Wait for all of the threads to finish.
# Note: KeyboardInterrupt will not be raised inside of a call to join()
# with no timeout. So we set an arbitrarily large timeout, which
# (for whatever reason) allows KeyboardInterrupt to be raised.
while threads:
for t in list(threads): # Iterate over a copy
try:
t.join(1000) # Arbitrary timeout value
if not t.isAlive(): # If thread finished (join didn't time-out),
threads.remove(t) # We'll no longer join() on it
except KeyboardInterrupt: # If we get a Ctrl+C,
keep_running = False # Set global flag, telling threads to stop looping
if __name__ == '__main__':
main()
为什么这比(尽管它在美学上不如)好,而事实是:通过?因为这将使CPU忙着等待Ctrl+C,占用其他线程宝贵的执行时间。相反,此解决方案将允许其他线程运行,只有在按下Ctrl+C时才会唤醒
关于使用线程处理Ctrl+C的其他问题:
- ,它独立地提出了与我相同的解决方案
我还应该指出,
随机运行
并没有做到它的名字所暗示的那样;相反,它每次迭代的睡眠时间总是相同的。当程序启动时,这个数量是随机决定的。更好的解决方案是使用模块,它为线程提供类-一个面向对象的接口
这样,我们就可以轻松地运行三个线程(等待它们完成)
调整您的示例:
import threading
import time
import random
keep_running = True
def runOften(threadName, sleepTime):
while keep_running:
time.sleep(sleepTime)
print "%s" %(threadName)
def runLessOften(threadName, sleepTime):
while keep_running:
time.sleep(sleepTime)
print "%s" %(threadName)
def runRandomly(threadName, sleepTime):
while keep_running:
time.sleep(sleepTime)
print "%s" %(threadName)
def main():
global keep_running
# Create the threads
threads = []
threads.append(threading.Thread(target=runOften, args=("Often Runs", 2)))
threads.append(threading.Thread(target=runLessOften, args=("Less Often Runs!", 2)))
threads.append(threading.Thread(target=runRandomly, args=("Fast and random", random.random())))
# Start the threads
for t in threads:
t.start()
# Wait for all of the threads to finish.
# Note: KeyboardInterrupt will not be raised inside of a call to join()
# with no timeout. So we set an arbitrarily large timeout, which
# (for whatever reason) allows KeyboardInterrupt to be raised.
while threads:
for t in list(threads): # Iterate over a copy
try:
t.join(1000) # Arbitrary timeout value
if not t.isAlive(): # If thread finished (join didn't time-out),
threads.remove(t) # We'll no longer join() on it
except KeyboardInterrupt: # If we get a Ctrl+C,
keep_running = False # Set global flag, telling threads to stop looping
if __name__ == '__main__':
main()
为什么这比(尽管它在美学上不如)好,而事实是:通过?因为这将使CPU忙着等待Ctrl+C,占用其他线程宝贵的执行时间。相反,此解决方案将允许其他线程运行,只有在按下Ctrl+C时才会唤醒
关于使用线程处理Ctrl+C的其他问题:
- ,它独立地提出了与我相同的解决方案
我还应该指出,
随机运行
并没有做到它的名字所暗示的那样;相反,它每次迭代的睡眠时间总是相同的。该数量在程序启动时随机决定。我将在这里介绍OP使用应用程序框架呈现的代码行为的不同实现,因为:
- 它很优雅
- 它起作用了
- 它以
^C
- 它不使用线程
- 它说明了同样的行为
$ python foo.py
Run Less Often
Run Often
Run Randomly
Run Less Often
Run Less Often
Run Often
代码:
我将在这里介绍OP使用应用程序框架呈现的代码行为的不同实现,因为:
- 它很优雅
- 它起作用了
- 它以
^C
- 它不使用线程
- 它说明了同样的行为
$ python foo.py
Run Less Often
Run Often
Run Randomly
Run Less Often
Run Less Often
Run Often
代码:
+1用于
螺纹。螺纹(显然)。但我不确定我是否喜欢你的方法,原因很简单,如果不终止程序或使用^\
:)@JamesMills仍在试图以优雅的方式处理它。值得注意的是,KeyboardInterrupt
似乎不会从调用.join()
的内部引发。老实说,我不确定是否会引发。(螺纹吸)。如果你使用电路,你可以用一种优雅的方式来实现这一点(你必须以线程的形式启动每个组件(这真是一个愚蠢的想法),但至少系统会很好地终止。@詹姆斯米尔最终找到了一个可行的解决方案,尽管“优雅”可能不是这个词的意思。OP程序使用电路的行为的一个类似的人为例子:--在OP没有任何进一步的细节的情况下,这个人为程序的行为都与计时器和事件有关:)+1用于线程。Thread
(显然)。但我不确定我是否喜欢您的方法,原因很简单,如果不杀死程序或使用^\
:)很难终止程序