最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python多線程編程簡(jiǎn)單介紹

 更新時(shí)間:2015年04月13日 09:17:07   投稿:junjie  
這篇文章主要介紹了Python多線程編程簡(jiǎn)單介紹,本文講解了創(chuàng)建線程、Thread對(duì)象函數(shù)、常用示例等內(nèi)容,需要的朋友可以參考下

創(chuàng)建線程

格式如下

復(fù)制代碼 代碼如下:

threading.Thread(group=None, target=None, name=None, args=(), kwargs={})

這個(gè)構(gòu)造器必須用關(guān)鍵字傳參調(diào)用
- group 線程組
- target 執(zhí)行方法
- name 線程名字
- args target執(zhí)行的元組參數(shù)
- kwargs target執(zhí)行的字典參數(shù)

Thread對(duì)象函數(shù)

函數(shù) 描述
start() 開始線程的執(zhí)行
run() 定義線程的功能的函數(shù)(一般會(huì)被子類重寫)
join(timeout=None) 程序掛起,直到線程結(jié)束;如果給了 timeout,則最多阻塞 timeout 秒
getName() 返回線程的名字
setName(name) 設(shè)置線程的名字
isAlive() 布爾標(biāo)志,表示這個(gè)線程是否還在運(yùn)行中
isDaemon() 返回線程的 daemon 標(biāo)志
setDaemon(daemonic) 把線程的 daemon 標(biāo)志設(shè)為 daemonic(一定要在調(diào)用 start()函數(shù)前調(diào)用)

常用示例

格式

復(fù)制代碼 代碼如下:

import threading

def run(*arg, **karg):
    pass
thread = threading.Thread(target = run, name = "default", args = (), kwargs = {})
thread.start()


實(shí)例
復(fù)制代碼 代碼如下:

#!/usr/bin/python
#coding=utf-8

import threading
from time import ctime,sleep

def sing(*arg):
    print "sing start: ", arg
    sleep(1)
    print "sing stop"


def dance(*arg):
    print "dance start: ", arg
    sleep(1)
    print "dance stop"

threads = []

#創(chuàng)建線程對(duì)象
t1 = threading.Thread(target = sing, name = 'singThread', args = ('raise me up',))
threads.append(t1)

t2 = threading.Thread(target = dance, name = 'danceThread', args = ('Rup',))
threads.append(t2)

#開始線程
t1.start()
t2.start()

#等待線程結(jié)束
for t in threads:
    t.join()

print "game over"


輸出
復(fù)制代碼 代碼如下:

sing start:  ('raise me up',)
dance start:  ('Rup',)
sing stop
dance stop
game over

相關(guān)文章

最新評(píng)論

海晏县| 临颍县| 永吉县| 搜索| 汝阳县| 大荔县| 东兴市| 麻栗坡县| 济源市| 汉阴县| 天台县| 万宁市| 红河县| 苗栗县| 麻栗坡县| 怀柔区| 康平县| 宿迁市| 沁水县| 莱州市| 小金县| 北海市| 获嘉县| 绥芬河市| 正镶白旗| 五寨县| 尼木县| 信宜市| 永丰县| 桦甸市| 安龙县| 建阳市| 民和| 岢岚县| 莒南县| 凉山| 长顺县| 满洲里市| 绥德县| 徐州市| 筠连县|