Python3 threading 模块

thread = threading.Thread(target=None, name=None, daemon=None, group=None, args=(), kwargs={})
# 实例化一个线程
# target是线程调用run()方法的时候会调用的函数
# 参数args和kwargs分别表示调用target时的参数列表和关键字参数
# name是该线程名称
# daemon=True时,thread dies when main thread (only non-daemon thread) exits.

thread.start() # 一个线程最多只能调用该方法一次,如果多次调用则会报RuntimeError错误。它会调用run方法
thread.run() # 在这里运行线程的具体任务
thread.join(timeout=None) # 阻塞全部线程直到当前线程任务结束,timeout为阻塞时间,None时会一直阻塞

thread.name
thread.getName()
thread.setName()

thread.is_alive() # 判断当前进程是否存活

threading.active_count() # 返回当前线程对象Thread的个数
threading.current_thread() # 返回当前的线程对象Thread
threading.current_thread().name # 返回当前线程的名称

Python
# -*- coding: utf-8 -*- __author__ = 'songhao' import time, threading # 新线程执行的代码: def loop(): print('thread %s is running...' % threading.current_thread().name) n = 0 while n < 3: n = n + 1 print('thread %s >>> %s' % (threading.current_thread().name, n)) time.sleep(1) print('thread %s ended.' % threading.current_thread().name) print('thread %s is running...' % threading.current_thread().name) for x in range(3): t = threading.Thread(target=loop, name='LoopThread %s'% str(x)) t.start() t.join() print('thread %s ended.' % threading.current_thread().name) ###output### ➜ d3 python3 c7.py thread MainThread is running... thread LoopThread 0 is running... thread LoopThread 0 >>> 1 thread LoopThread 0 >>> 2 thread LoopThread 0 >>> 3 thread LoopThread 0 ended. thread LoopThread 1 is running... thread LoopThread 1 >>> 1 thread LoopThread 1 >>> 2 thread LoopThread 1 >>> 3 thread LoopThread 1 ended. thread LoopThread 2 is running... thread LoopThread 2 >>> 1 thread LoopThread 2 >>> 2 thread LoopThread 2 >>> 3 thread LoopThread 2 ended. thread MainThread ended.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# -*- coding: utf-8 -*-
__author__ = 'songhao'
 
import time , threading
 
 
# 新线程执行的代码:
def loop ( ) :
     print ( 'thread %s is running...' % threading . current_thread ( ) . name )
     n = 0
     while n < 3 :
         n = n + 1
         print ( 'thread %s >>> %s' % ( threading . current_thread ( ) . name , n ) )
         time . sleep ( 1 )
     print ( 'thread %s ended.' % threading . current_thread ( ) . name )
 
 
print ( 'thread %s is running...' % threading . current_thread ( ) . name )
for x in range ( 3 ) :
     t = threading . Thread ( target = loop , name = 'LoopThread %s' % str ( x ) )
     t . start ( )
     t . join ( )
print ( 'thread %s ended.' % threading . current_thread ( ) . name )
 
###output###
➜    d3 python3 c7 . py
thread MainThread is running . . .
thread LoopThread 0 is running . . .
thread LoopThread 0 >>> 1
thread LoopThread 0 >>> 2
thread LoopThread 0 >>> 3
thread LoopThread 0 ended .
thread LoopThread 1 is running . . .
thread LoopThread 1 >>> 1
thread LoopThread 1 >>> 2
thread LoopThread 1 >>> 3
thread LoopThread 1 ended .
thread LoopThread 2 is running . . .
thread LoopThread 2 >>> 1
thread LoopThread 2 >>> 2
thread LoopThread 2 >>> 3
thread LoopThread 2 ended .
thread MainThread ended .

例子1.以线程的方式启动和创建一个函数

Python
# -*- coding: utf-8 -*- import time __author__ = 'songhao' import threading def clock(kk): while True: print(time.ctime()) time.sleep(kk) t = threading.Thread(target=clock, args=(10,)) t.setDaemon = True // 设置线程的布尔型后台标志 t.start()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# -*- coding: utf-8 -*-
import time
 
__author__ = 'songhao'
import threading
 
 
def clock ( kk ) :
     while True :
         print ( time . ctime ( ) )
         time . sleep ( kk )
 
 
t = threading . Thread ( target = clock , args = ( 10 , ) )
t . setDaemon = True / / 设置线程的布尔型后台标志
t . start ( )

t.setDaemon = True
// 设置线程的布尔型后台标志,必须在调用t.run() 方法之前,设置这个标志,

Python
# -*- coding: utf-8 -*- __author__ = 'songhao' import threading import time class NewDemo(threading.Thread): def __init__(self,kk): threading.Thread.__init__(self) self.daemon = True self.kk = kk def run(self): while True: print(time.ctime()) time.sleep(self.kk) if __name__ == '__main__': n = NewDemo(10) n.start() n.jion() // 等到线程终止或者出现超时为止
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# -*- coding: utf-8 -*-
__author__ = 'songhao'
import threading
import time
class NewDemo ( threading . Thread ) :
     def __init__ ( self , kk ) :
         threading . Thread . __init__ ( self )
         self . daemon = True
         self . kk = kk
 
     def run ( self ) :
         while True :
             print ( time . ctime ( ) )
             time . sleep ( self . kk )
 
 
if __name__ == '__main__' :
     n = NewDemo ( 10 )
     n . start ( )
     n . jion ( ) / / 等到线程终止或者出现超时为止



  • zeropython 微信公众号 5868037 QQ号 5868037@qq.com QQ邮箱
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值