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

Python多線程編程簡單介紹

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

創(chuàng)建線程

格式如下

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

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

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

Thread對象函數(shù)

函數(shù) 描述
start() 開始線程的執(zhí)行
run() 定義線程的功能的函數(shù)(一般會被子類重寫)
join(timeout=None) 程序掛起,直到線程結(jié)束;如果給了 timeout,則最多阻塞 timeout 秒
getName() 返回線程的名字
setName(name) 設(shè)置線程的名字
isAlive() 布爾標(biāo)志,表示這個線程是否還在運(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)建線程對象
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)文章

最新評論

曲麻莱县| 剑河县| 沙雅县| 清河县| 德格县| 米脂县| 大安市| 安化县| 陆丰市| 苏尼特右旗| 西藏| 丰原市| 伊金霍洛旗| 沾益县| 衡南县| 彝良县| 蛟河市| 东港市| 赤水市| 东宁县| 于田县| 涞水县| 玉环县| 蕉岭县| 清涧县| 曲松县| 刚察县| 章丘市| 高要市| 华亭县| 嫩江县| 黄龙县| 疏附县| 宕昌县| 吴旗县| 巍山| 南江县| 祁阳县| 平遥县| 古蔺县| 柳林县|