我如何在python中进行线程化?

Ric*_*ard 2 python multithreading

使用本网站的代码:http://www.saltycrane.com/blog/2008/09/simplistic-python-thread-example/

代码是

import time
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()
Run Code Online (Sandbox Code Playgroud)

我得到这个输出:

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 4
finished sleeping from thread 1finished sleeping from thread 6finished sleeping from   thread 8
finished sleeping from thread 5finished sleeping from thread 7finished sleeping from thread 9

finished sleeping from thread 3
Run Code Online (Sandbox Code Playgroud)

这里发生了什么?我没有按顺序打印的线程,因为这是可能的,但为什么它们最后不在新行上打印?我在windows xp下使用python 2.6

Kar*_*tan 6

你刚才发现为什么用线程编程很难:)

发生的事情是你的所有线程几乎在同一时间被唤醒.一个线程开始打印出"从线程1完成休眠",在它有机会打印最后一个"\n"之前,另一个线程出现并打印"从线程6完成休眠",然后打开.这些换行没有被跳过,它们只是四处移动并在其他地方聚集起来.这可能就是为什么在"完成...... 3"之前跳过了一条线.我的猜测是,由于格式化,有许多尾随空行被删除.

使用threading.Lock在您的print语句周围放置同步,以便多个prints不能同时发生.


Ale*_*nor 5

打印由多个操作码实现,特别是换行符是单独的操作码.Python将在操作码之间切换上下文:

>>> def f(o):
...     print o
...     
...     

>>> from dis import dis

>>> dis(f)
  2           0 LOAD_FAST                0 (o)
              3 PRINT_ITEM          
              4 PRINT_NEWLINE       
              5 LOAD_CONST               0 (None)
              8 RETURN_VALUE        
Run Code Online (Sandbox Code Playgroud)