python執行緒學習

2021-06-19 18:29:40 字數 4178 閱讀 2003

#!/usr/bin/python

#coding:utf-8

import time

# threading 提供了乙個比thread模組更高層的api模組來提供執行緒的併發性。

# 下面表示乙個程式的執行(這個例子需要5秒才能執行完成效率並不是很好)

"""

def worker(i):

print 'worker%d'%i

time.sleep(1)

return

if __name__ == '__main__':

aa = time.time()

for x in xrange(5):

worker(x)

print time.time() - aa

# ############################

# 執行結果 :

# >>>

# worker0

# worker1

# worker2

# worker3

# worker4

# 5.04699993134

# ################################

""""""

# 下面的例子的是使用多執行緒

import threading

def worker(i):

print 'worker%d'%i

time.sleep(1)

return

if __name__ == '__main__':

aa = time.time()

for x in xrange(5):

t = threading.thread(target=worker,args=(x,)) # target 是目標函式, args是傳來的引數(用的元祖的形式進行傳參)

t.start()

print time.time() - aa

#可以看到效率是很快的

# ############################

# 執行結果:

# worker0

# worker1

# worker2

# worker3

# worker4

# 0.000999927520752

# ############################

""""""

#2 threading.activecount()的使用,此方法返回當前程序中線程的個數。返回的個數中包含主線程

import threading

import time

def worker(i):

print 'worker%d'%i

time.sleep(i)

return

if __name__ == '__main__':

aa = time.time()

for i in xrange(6):

t = threading.thread(target=worker,args=(i,))

t.start()

print time.time() - aa

print u'執行緒總數',threading.activecount() - 1

""""""

# threading.enumerate()的使用。此方法返回當前執行中的thread物件列表

import threading

import time

def worker(i):

print 'worker%d'%i

time.sleep(1)

return

if __name__ == '__main__':

threads =

for x in xrange(5):

t = threading.thread(target=worker,args=(x,))

t.start()

for item in threading.enumerate():

print item

print "#################"

for v in threads:

print v

""""""

# threading.setdaemon()的使用。設定後台程序。

def worker(i):

time.sleep(1)

print 'worker%s'%str(i)

if __name__ == '__main__':

for i in xrange(5):

t= threading.thread(target=worker,args=(i,))

t.setdaemon(true)

t.start()

print 'haha'

"""

import threading , time

# 繼承執行緒函式

class mythread(threading.thread):

"""派生出的子執行緒 """

def __init__(self,func,args,name=''):

threading.thread.__init__(self) #初始化基類

# 初始化函式

self.func = func

#初始化引數

self.args = args

#初始化名字

self.name = name

# 重新寫執行緒的run函式

def run(self):

def threads(nloop,nsec):

print 'start loop',nloop,'at',time.ctime()

time.sleep(nsec)

print 'loop',nloop,'done at',time.ctime()

def main():

print 'starting at',time.ctime()

ths =

nloops = range(5)

#建立執行緒物件

for i in nloops:

t = mythread(threads,(i,i),threads.__name__)

# 啟動執行緒

for i in nloops:

ths[i].start()

#等待子執行緒結束

for i in nloops:

ths[i].join()

print 'all done at',time.ctime()

if __name__ == '__main__':

main()

# ############################

# >>>

# starting at tue dec 17 14:15:09 2013

# start loop 0 at tue dec 17 14:15:09 2013

# loop 0 done at tue dec 17 14:15:09 2013

# start loop 1 at tue dec 17 14:15:09 2013

# start loop 2 at tue dec 17 14:15:09 2013

# start loop 3 at tue dec 17 14:15:09 2013

# start loop 4 at tue dec 17 14:15:09 2013 上面標示執行緒同時進行了白哦是你應該開器了執行緒

# loop 1 done at tue dec 17 14:15:10 2013

# loop 2 done at tue dec 17 14:15:11 2013

# loop 3 done at tue dec 17 14:15:12 2013

# loop 4 done at tue dec 17 14:15:13 2013

# all done at tue dec 17 14:15:13 2013

# ############################

Python多執行緒學習

一 建立執行緒 1 通過thread模組中的start new thread func,args 建立執行緒 在eclipse pydev中敲出以下 coding utf 8 import thread def run thread n for i in range n print i thread...

Python多執行緒學習

首先了解一下單執行緒,在啊很多年前的ms dos時代,作業系統處理問題都是單任務的,我想做聽 和看電影兩件事兒,那麼一定要先排一下順序。from time import ctime,sleep defmusic for i in range 2 print i was listening to mu...

python執行緒學習筆記

1.執行緒建立 1.1函式建立執行緒 def helloworld time.sleep 2 print hello world t threading.thread target helloworld t.start print main thread 1.2類建立執行緒 class hellow...