python threads,threading的用法
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
import threading,timefrom time import ctime,sleepclass A(object): def b(self): print ctime() time.sleep(3) print ctime() def c(self): print ctime() time.sleep(4) print ctime() def process(self): #args是关键字参数,需要加上名字,写成args=(self,) threads = [] t1 = threading.Thread(target=A.b,args=(self,)) threads.append(t1) t2 = threading.Thread(target=A.c,args=(self,)) threads.append(t2) for t in threads: # t.setDaemon(True) t.start() # t.join()if __name__ == '__main__': P = A() P.process() |
?
|
1
|
?
|
1
|
在非类中调用方法 |
?
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
|
<pre class="brush: py;">#coding=utf-8import threading,timefrom time import ctime,sleepdef music(func): for i in range(2): print "I was listening to %s. %s" %(func,ctime()) sleep(1)def move(func): for i in range(2): print "I was at the %s! %s" %(func,ctime()) sleep(5)threads = []t1 = threading.Thread(target=music,args=(u'cadillac',))threads.append(t1)t2 = threading.Thread(target=move,args=(u'lexus',))threads.append(t2)if __name__ == '__main__': for t in threads: t.setDaemon(True) t.start() print "all over %s" %ctime()</pre><br> |
收藏
文章评论,共0条
赞 (0)
