欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python3多線程操作簡單示例

 更新時間:2018年05月22日 11:04:01   作者:愛代碼也愛生活  
這篇文章主要介紹了Python3多線程操作,結(jié)合實例形式分析了Python3兼容Python2使用_thread進(jìn)行多線程操作的簡單實現(xiàn)技巧,需要的朋友可以參考下

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

python3 線程中常用的兩個模塊為:

_thread

threading(推薦使用)

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

test.py

# -*- coding:utf-8 -*-
#!/usr/bin/python3
import _thread
import 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個線程
try:
  _thread.start_new_thread( echo_name,("thread_1",2))
  _thread.start_new_thread( echo_name,("thread_2",5))
except:
  print("error:無法啟動線程")
#死循環(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

更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python進(jìn)程與線程操作技巧總結(jié)》、《Python Socket編程技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總

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

相關(guān)文章

最新評論