如何在Python中编写线程等待

时间:2015-12-24 11:23:34

标签: python multithreading

我有一个线程,我想设置等待超时,或来自主线程的信号。

我想出了这个。它不起作用。为什么呢?

curl -u admin:admin 'http://localhost:4502/system/console/configMgr/org.apache.sling.engine.impl.auth.SlingAuthenticator' --data 'apply=true&action=ajaxConfigManager&%24location=launchpad%3Aresources%2Finstall%2F19%2Forg.apache.sling.auth.core-1.3.6.jar&auth.sudo.cookie=sling.sudo&auth.sudo.parameter=sudo&auth.annonymous=false&sling.auth.requirements=%2B%2F&sling.auth.requirements=-%2Flibs%2Fgranite%2Fcore%2Fcontent%2Flogin&sling.auth.requirements=-%2Fetc%2Fclientlibs%2Fgranite&sling.auth.anonymous.user=&sling.auth.anonymous.password=unmodified&auth.http=preemptive&auth.http.realm=Sling+(Development)&auth.uri.suffix=%2Fj_security_check&propertylist=auth.sudo.cookie%2Cauth.sudo.parameter%2Cauth.annonymous%2Csling.auth.requirements%2Csling.auth.anonymous.user%2Csling.auth.anonymous.password%2Cauth.http%2Cauth.http.realm%2Cauth.uri.suffix'

2 个答案:

答案 0 :(得分:2)

你没有在主线程中释放锁。 释放它,它会工作(线程在wait存在后重新获取锁定,但由于它已被获取,因此线程被锁定。)

# Start new Threads
thread1.start()
time.sleep(2)

t1.acquire()
cond.notifyAll()
t1.release()

答案 1 :(得分:1)

time.sleep(2)等待的时间少于cond.wait(10),但仍会获得t1

您应该删除time.sleep(2)并将其替换为thread1.join(),以等待thread1完成。

  

<强>加入([超时])

     

等到线程终止。这会阻塞调用线程直到   调用join()方法的线程终止 - 正常   或通过未处理的异常 - 或直到可选的超时   发生。   (ref