python线程不起作用

时间:2015-02-10 20:52:53

标签: python multithreading

我的代码在这里。

import time
import thread

def myfunction(sleeptime,lock,*args):
    count = 0
    while 1:
    #entering critical section
        lock.acquire()
        count +=1 
        print count," Now Sleeping after Lock acquired for ",sleeptime
        time.sleep(sleeptime) 
        print count," Now releasing lock "
        lock.release()

if __name__=="__main__":

    lock=thread.allocate_lock()
    thread.start_new_thread(myfunction,(2,lock))
    #thread.start_new_thread(myfunction,("Thread No:2",2,lock))

    while 1:pass

这里我尝试创建一个简单的线程并计算它们,但我的代码显示了一些语法错误,看起来就是这样。

 File "thread_example.py", line 6
    while 1:
           ^
IndentationError: unindent does not match any outer indentation level

但是当我从myfunction()中移除计数时,我的代码可以正常工作,但是我不明白其中的错误。

另一个问题: 作为python中的一个新东西,我尝试学习thread。上面代码中的更多变化看起来像这样

import time
import thread

def myfunction(string,sleeptime,lock,*args):
    while 1:
        lock.acquire() 
        print string," Now Sleeping after Lock acquired for ",sleeptime
        time.sleep(sleeptime) 
        print string," Now releasing lock and then sleeping again"
        lock.release()

if __name__=="__main__":

    lock=thread.allocate_lock()
    thread.start_new_thread(myfunction,("Thread No:1",2,lock))
    thread.start_new_thread(myfunction,("Thread No:2",2,lock))

    while 1:pass

在跑步之前我认为我按顺序获得了线程No:1 线程No:2 ,但我的输出是这个

Thread No:1  Now Sleeping after Lock acquired for  2
Thread No:1  Now releasing lock and then sleeping again
Thread No:2  Now Sleeping after Lock acquired for  2
Thread No:2  Now releasing lock and then sleeping again
Thread No:1  Now Sleeping after Lock acquired for  2
Thread No:1  Now releasing lock and then sleeping again
Thread No:1  Now Sleeping after Lock acquired for  2
Thread No:1  Now releasing lock and then sleeping again
Thread No:2  Now Sleeping after Lock acquired for  2
Thread No:2  Now releasing lock and then sleeping again
Thread No:1  Now Sleeping after Lock acquired for  2
Thread No:1  Now releasing lock and then sleeping again
Thread No:1  Now Sleeping after Lock acquired for  2
Thread No:1  Now releasing lock and then sleeping again
Thread No:1  Now Sleeping after Lock acquired for  2
Thread No:1  Now releasing lock and then sleeping again
Thread No:1  Now Sleeping after Lock acquired for  2

这对我来说也是不可理解的。所以为什么它得到线程号:1这么多时间?

对于python中任何学习线程的建议对我都有帮助。

1 个答案:

答案 0 :(得分:0)

myfunction中,打印的消息显示&#34;释放锁定,然后再次睡眠&#34;,但代码没有第二次睡眠。< / p>

清理这两个问题:

import time
import thread

def myfunction(string,sleeptime,lock,*args):
    count = 0   
    while 1:
        lock.acquire()
        count += 1
        print "{} {} Now Sleeping after Lock acquired for {}".format(string, count, sleeptime)
        time.sleep(sleeptime) 
        print "{} {} Now releasing lock and then sleeping again".format(string, count)
        lock.release()
        time.sleep(sleeptime)

if __name__=="__main__":

    lock=thread.allocate_lock()
    thread.start_new_thread(myfunction,("Thread No:1",0.5,lock))
    thread.start_new_thread(myfunction,("Thread No:2",0.5,lock))

    while 1:pass

输出结果为:

Thread No:1 1 Now Sleeping after Lock acquired for 0.5
Thread No:1 1 Now releasing lock and then sleeping again
Thread No:2 1 Now Sleeping after Lock acquired for 0.5
Thread No:2 1 Now releasing lock and then sleeping again
Thread No:1 2 Now Sleeping after Lock acquired for 0.5
Thread No:1 2 Now releasing lock and then sleeping again
Thread No:2 2 Now Sleeping after Lock acquired for 0.5
Thread No:2 2 Now releasing lock and then sleeping again
Thread No:1 3 Now Sleeping after Lock acquired for 0.5
Thread No:1 3 Now releasing lock and then sleeping again