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

首頁(yè) > 編程 > Python > 正文

Python3多線程操作簡(jiǎn)單示例

2020-01-04 15:00:51
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

本文實(shí)例講述了Python3多線程操作。分享給大家供大家參考,具體如下:

python3 線程中常用的兩個(gè)模塊為:

_thread

threading(推薦使用)

thread 模塊已被廢棄。用戶可以使用 threading 模塊代替。所以,在 python3 中不能再使用"thread" 模塊。為了兼容性,python3 將 thread 重命名為 "_thread"。

test.py

# -*- coding:utf-8 -*-#!/usr/bin/python3import _threadimport time# 定義線程調(diào)用函數(shù)def echo_name(tag,delay):  count=0  while count<5:    time.sleep(delay)    count+=1    print("%s:%s" % ( tag,time.ctime(time.time()) ))#創(chuàng)建2個(gè)線程try:  _thread.start_new_thread( echo_name,("thread_1",2))  _thread.start_new_thread( echo_name,("thread_2",5))except:  print("error:無(wú)法啟動(dòng)線程")#死循環(huán)while 1:  pass

執(zhí)行結(jié)果

[root@mail pythonCode]# python3 test.py
thread_1:Wed Jul 20 18:03:39 2016
thread_1:Wed Jul 20 18:03:41 2016
thread_2:Wed Jul 20 18:03:42 2016
thread_1:Wed Jul 20 18:03:43 2016
thread_1:Wed Jul 20 18:03:45 2016
thread_2:Wed Jul 20 18:03:47 2016
thread_1:Wed Jul 20 18:03:47 2016
thread_2:Wed Jul 20 18:03:52 2016
thread_2:Wed Jul 20 18:03:57 2016
thread_2:Wed Jul 20 18:04:02 2016

 

希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到python教程頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 万荣县| 沾益县| 长寿区| 巴塘县| 武冈市| 江津市| 宣武区| 舟山市| 米易县| 东光县| 乾安县| 亚东县| 拜城县| 阿合奇县| 汾西县| 万全县| 郸城县| 普格县| 遵义县| 江门市| 奈曼旗| 伊吾县| 美姑县| 南木林县| 瑞金市| 普洱| 乌拉特前旗| 南溪县| 阜城县| 怀远县| 开远市| 皮山县| 景谷| 玉溪市| 泸水县| 信阳市| 沙洋县| 龙游县| 长兴县| 集贤县| 中方县|