Python线程概念与实例

algorain

Python线程概念与实例;Python线程概念与实例;Python线程概念与实例;Python线程概念与实例;Python线程概念与实例;Python线程概念与实例;Python线程概念与实例;Python线程概念与实例;

在单线程中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# -*- coding=utf8 -*-
import threading
from time import ctime, sleep
#单线程实例
def frist(frist_name, length):
for i in range(4):
print('I was listening to %s %s'% (frist_name, ctime()))
sleep(length)
def second(second_name, length):
for i in range(2):
print('I was at the movie %s %s'% (second_name, ctime()))
sleep(length)
if __name__ == '__main__':
frist('第一线程', 2)
second('第二线程', 4)
print('all over %s'% ctime())
</pre>

结果

1
2
3
4
5
6
7
I was listening to 第一线程 Tue Jan 17 23:13:07 2017
I was listening to 第一线程 Tue Jan 17 23:13:09 2017
I was listening to 第一线程 Tue Jan 17 23:13:11 2017
I was listening to 第一线程 Tue Jan 17 23:13:13 2017
I was at the movie 第二线程 Tue Jan 17 23:13:15 2017
I was at the movie 第二线程 Tue Jan 17 23:13:19 2017
all over Tue Jan 17 23:13:23 2017

多线程中

  • 在多线程中,线程的调用时随机的,所以每次的结果并不一样
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# -*- coding=utf8 -*-
import threading
from time import ctime, sleep
#多线程
def frist(frist_name, length):
for i in range(4):
print('I was listening to %s %s'% (frist_name, ctime()))
sleep(length)
def second(second_name, length):
for i in range(2):
print('I was at the second %s %s'% (second_name, ctime()))
sleep(length)
th1 = threading.Thread(target=frist, args=('第一线程', 1))
th2 = threading.Thread(target=second, args=('第二线程', 2))
threads = [th1,th2]

if __name__ == '__main__':
for t in threads:
t.setDaemon(True)
t.start()
t.join()
print('all over %s' % ctime())</pre>

####结果

1
2
3
4
5
6
7
I was listening to 第一线程 Tue Jan 17 23:25:22 2017
I was at the second 第二线程 Tue Jan 17 23:25:22 2017
I was listening to 第一线程 Tue Jan 17 23:25:23 2017
I was listening to 第一线程 Tue Jan 17 23:25:24 2017
I was at the second 第二线程 Tue Jan 17 23:25:24 2017
I was listening to 第一线程 Tue Jan 17 23:25:25 2017
all over Tue Jan 17 23:25:26 2017
  • threading是Python标准库中的模块,有些朋友查到会有thread这个模块,但是在python3里面只剩下threading这个模块了,因为threading模块用起来更简单也更安全一些
  1. time模块中有ctime(),sleep()等函数,ctime()返回当前时间,用一个str表示,sleep(n)表示挂起n秒

  2. import threading 导入threading模块

  3. th1 = threading.Thread(target=music, args=(‘第一线程’,1)) 创建一个线程th1,threading.Thread()是一个类,类的构造函数原型如下: class threading.Thread(group=None,target=None, name=None, args=(), kwargs={}, *, daemon=None) 这里用到了target,表示要调用的函数名,args表示调用函数的参数

1
2
3
4
5
6
7
8
threads = [th1, th2]
将两个线程放入一个列表中
for t in threads:
t.setDaemon(True)
t.start()
t.join()
最后使用一个for循环,依次将列表中的线程开启
t.setDaemon(True)

将线程声明为守护线程,必须在start() 方法调用之前设置,如果不设置为守护线程程序会被无限挂起。子线程启动后,父线程也继续执行下去,当父线程执行完最后一条语句print “all over %s” %ctime()后,没有等待子线程,直接就退出了,同时子线程也一同结束。 t.start() 开始线程活动。 t.join()

  • join()方法,用于等待线程终止。join()的作用是,在子线程完成运行之前,这个子线程的父线程将一直等待。

注意: join()方法的位置是在for循环外的,也就是说必须等待for循环里的两个进程都结束后,才去执行主进程。如果没有join()函数,那么父线程执行完之后就会立即结束,不会等待子线程执行完

  • Title: Python线程概念与实例
  • Author: algorain
  • Created at: 2017-02-01 08:20:10
  • Updated at: 2023-05-14 21:39:50
  • Link: http://www.rain1024.com/2017/02/01/python-article0004/
  • License: This work is licensed under CC BY-NC-SA 4.0.
 Comments