国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python多線程編程簡單介紹

2019-11-25 17:46:02
字體:
來源:轉載
供稿:網友

創建線程

格式如下

復制代碼 代碼如下:

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

這個構造器必須用關鍵字傳參調用
- group 線程組
- target 執行方法
- name 線程名字
- args target執行的元組參數
- kwargs target執行的字典參數

Thread對象函數

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

常用示例

格式

復制代碼 代碼如下:

import threading

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


實例
復制代碼 代碼如下:

#!/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 = []

#創建線程對象
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()

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

print "game over"


輸出
復制代碼 代碼如下:

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 井研县| 全州县| 红桥区| 乌拉特中旗| 宜都市| 普陀区| 福海县| 云安县| 和龙市| 沅江市| 聊城市| 宣汉县| 阳城县| 高雄县| 天津市| 九寨沟县| 甘孜| 镇平县| 蓝山县| 安西县| 商城县| 华亭县| 舞阳县| 长宁县| 甘谷县| 万全县| 乐安县| 陕西省| 保亭| 宜黄县| 卫辉市| 洪雅县| 广德县| 上饶县| 读书| 绍兴县| 梁平县| 彰化县| 武安市| 灯塔市| 汤原县|