python 多线程 threading
2017-09-04 11:32
375 查看
(python提供了两个模块来实现多线程thread 和threading ,thread 有一些缺点,在threading 得到了弥补,为了不浪费你和时间,所以我们直接学习threading 就可以了。)
# 单线程 我想做听音乐和看电影两件事儿,那么一定要先排一下顺序。
from time import ctime, sleep
def music():
for i in range(2):
print("I was listening to music. %s" % ctime())
sleep(2)
def move():
for i in range(2):
print("I was at the movies! %s" % ctime())
sleep(5)
if __name__ == '__main__':
music()
move()
print("all over %s" % ctime())
=========================== RESULT ================================
I was listening to music. Mon Sep 4 11:00:08 2017
I was listening to music. Mon Sep 4 11:00:10 2017
I was at the movies! Mon Sep 4 11:00:12 2017
I was at the movies! Mon Sep 4 11:00:17 2017
all over Mon Sep 4 11:00:22 2017
# 单线程 music()和move()更应该被看作是音乐和视频播放器,至于要播放什么歌曲和视频应该由我们使用时决定
import threading
from time import ctime, sleep
def music(func):
for i in range(2):
print("I was listening to %s. %s" % (func, ctime()))
sleep(2)
def move(func):
for i in range(2):
print("I was at the %s! %s" % (func, ctime()))
sleep(5)
if __name__ == '__main__':
music("一生所爱")
move("敦刻尔克")
print("all over %s" % ctime())
=========================== RESULT ================================
I was listening to 一生所爱. Mon Sep 4 11:12:39 2017
I was listening to 一生所爱. Mon Sep 4 11:12:40 2017
I was at the 敦刻尔克! Mon Sep 4 11:12:42 2017
I was at the 敦刻尔克! Mon Sep 4 11:12:47 2017
all over Mon Sep 4 11:12:52 2017
# 多线程 引入threadring来同时播放音乐和视频
import threading
from time import ctime, sleep
def music(func):
for i in range(2):
print("I was listening to %s. %s" % (func, ctime()))
sleep(2)
def move(func):
for i in range(2):
print("I was at the %s! %s" % (func, ctime()))
sleep(5)
threads = []
t1 = threading.Thread(target=music, args=("一生所爱",))
threads.append(t1)
t2 = threading.Thread(target=move, args=("敦刻尔克",))
threads.append(t2)
if __name__ == '__main__':
for t in threads:
t.setDaemon(True)
t.start()
print("all over %s" % ctime())
=========================== RESULT ================================
I was listening to 一生所爱. Mon Sep 4 11:21:01 2017
I was at the 敦刻尔克! Mon Sep 4 11:21:01 2017
all over Mon Sep 4 11:21:01 2017
注释:从执行结果来看,子线程(muisc 、move )和主线程(print "all over %s" %ctime())都是同一时间启动,但由于主线程执行完结束,所以导致子线程也终止。
# 多线程 继续调整程序
# 对上面的程序加了个join()方法,用于等待线程终止。join()的作用是,在子线程完成运行之前,这个子线程的父线程将一直被阻塞
# join()方法的位置是在for循环外的,也就是说必须等待for循环里的两个进程都结束后,才去执行主进程
import threading
from time import ctime, sleep
def music(func):
for i in range(2):
print("I was listening to %s. %s" % (func, ctime()))
sleep(2)
def move(func):
for i in range(2):
print("I was at the %s! %s" % (func, ctime()))
sleep(5)
threads = []
t1 = threading.Thread(target=music, args=("一生所爱",))
threads.append(t1)
t2 = threading.Thread(target=move, args=("敦刻尔克",))
threads.append(t2)
if __name__ == '__main__':
for t in threads:
t.setDaemon(True)
t.start()
t.join()
print("all over %s" % ctime())
=========================== RESULT ================================
I was listening to 一生所爱. Mon Sep 4 11:24:56 2017
I was at the 敦刻尔克! Mon Sep 4 11:24:56 2017
I was listening to 一生所爱. Mon Sep 4 11:24:58 2017
I was at the 敦刻尔克! Mon Sep 4 11:25:01 2017
all over Mon Sep 4 11:25:06 2017
注:更详细的使用请参考其它文档或资料
https://docs.python.org/3/library/threading.html https://docs.python.org/2/library/threading.html
# 单线程 我想做听音乐和看电影两件事儿,那么一定要先排一下顺序。
from time import ctime, sleep
def music():
for i in range(2):
print("I was listening to music. %s" % ctime())
sleep(2)
def move():
for i in range(2):
print("I was at the movies! %s" % ctime())
sleep(5)
if __name__ == '__main__':
music()
move()
print("all over %s" % ctime())
=========================== RESULT ================================
I was listening to music. Mon Sep 4 11:00:08 2017
I was listening to music. Mon Sep 4 11:00:10 2017
I was at the movies! Mon Sep 4 11:00:12 2017
I was at the movies! Mon Sep 4 11:00:17 2017
all over Mon Sep 4 11:00:22 2017
# 单线程 music()和move()更应该被看作是音乐和视频播放器,至于要播放什么歌曲和视频应该由我们使用时决定
import threading
from time import ctime, sleep
def music(func):
for i in range(2):
print("I was listening to %s. %s" % (func, ctime()))
sleep(2)
def move(func):
for i in range(2):
print("I was at the %s! %s" % (func, ctime()))
sleep(5)
if __name__ == '__main__':
music("一生所爱")
move("敦刻尔克")
print("all over %s" % ctime())
=========================== RESULT ================================
I was listening to 一生所爱. Mon Sep 4 11:12:39 2017
I was listening to 一生所爱. Mon Sep 4 11:12:40 2017
I was at the 敦刻尔克! Mon Sep 4 11:12:42 2017
I was at the 敦刻尔克! Mon Sep 4 11:12:47 2017
all over Mon Sep 4 11:12:52 2017
# 多线程 引入threadring来同时播放音乐和视频
import threading
from time import ctime, sleep
def music(func):
for i in range(2):
print("I was listening to %s. %s" % (func, ctime()))
sleep(2)
def move(func):
for i in range(2):
print("I was at the %s! %s" % (func, ctime()))
sleep(5)
threads = []
t1 = threading.Thread(target=music, args=("一生所爱",))
threads.append(t1)
t2 = threading.Thread(target=move, args=("敦刻尔克",))
threads.append(t2)
if __name__ == '__main__':
for t in threads:
t.setDaemon(True)
t.start()
print("all over %s" % ctime())
=========================== RESULT ================================
I was listening to 一生所爱. Mon Sep 4 11:21:01 2017
I was at the 敦刻尔克! Mon Sep 4 11:21:01 2017
all over Mon Sep 4 11:21:01 2017
注释:从执行结果来看,子线程(muisc 、move )和主线程(print "all over %s" %ctime())都是同一时间启动,但由于主线程执行完结束,所以导致子线程也终止。
# 多线程 继续调整程序
# 对上面的程序加了个join()方法,用于等待线程终止。join()的作用是,在子线程完成运行之前,这个子线程的父线程将一直被阻塞
# join()方法的位置是在for循环外的,也就是说必须等待for循环里的两个进程都结束后,才去执行主进程
import threading
from time import ctime, sleep
def music(func):
for i in range(2):
print("I was listening to %s. %s" % (func, ctime()))
sleep(2)
def move(func):
for i in range(2):
print("I was at the %s! %s" % (func, ctime()))
sleep(5)
threads = []
t1 = threading.Thread(target=music, args=("一生所爱",))
threads.append(t1)
t2 = threading.Thread(target=move, args=("敦刻尔克",))
threads.append(t2)
if __name__ == '__main__':
for t in threads:
t.setDaemon(True)
t.start()
t.join()
print("all over %s" % ctime())
=========================== RESULT ================================
I was listening to 一生所爱. Mon Sep 4 11:24:56 2017
I was at the 敦刻尔克! Mon Sep 4 11:24:56 2017
I was listening to 一生所爱. Mon Sep 4 11:24:58 2017
I was at the 敦刻尔克! Mon Sep 4 11:25:01 2017
all over Mon Sep 4 11:25:06 2017
注:更详细的使用请参考其它文档或资料
https://docs.python.org/3/library/threading.html https://docs.python.org/2/library/threading.html
相关文章推荐
- Python标准库08 多线程与同步 (threading包)
- Python:使用threading模块实现多线程(转)
- Python2 socket 多线程并发 ThreadingTCPServer Demo
- Python:使用threading模块实现多线程
- python(13)多线程:线程池,threading
- Python:threading模块用于多线程控制和处理
- Python模块学习:threading 多线程控制和处理
- Python多线程:thread、threading、queue
- python模块threading实现多线程
- Python:使用threading模块实现多线程(转)
- Python threading 多线程控制和处理
- Python3.5 多线程简单例子--threading.Thread
- python--threading多线程总结
- Python标准库08 多线程与同步 (threading包)
- Python中多线程thread与threading的实现方法
- python3.6 - threading 多线程编程基础(1)
- “死锁” 与 python多线程之threading模块下的锁机制
- Python多线程thread与threading实现
- 4.python学习笔记:python多线程下的threading模块
- Python 多线程(threading模块)