A Basic Example of Threads Synchronization in Python, python中的线程同步示例
http://zulko.github.io/blog/2013/09/19/a-basic-example-of-threads-synchronization-in-python/
We will see how to use threading Events to have functions in different Python threads start at the same time.
I recently coded a method to view movies in Python : it plays the video, and in the same time, in a parralel thread, it renders the audio. The difficult part is that the audio and video should be exactly synchronized. The pseudo-code looks like this:
1 |
|
In this code, play_audio()
and play_video()
will start at approximately the same time and will run parallely, but these functions need some preparation before actually starting playing stuff. Their code looks like that:
1 |
|
To have a well-synchronized movie we need the internal functions audio.start_playing()
and video.start_playing()
, which are run in two separate threads, to start at exactly the same time. How do we do that ?
The solution seems to be using threading.Event
objects. An Event
is an object that can be accessed from all the threads and allows very basic communication between them : each thread can set or unset an Event, or check whether this event has already been been set (by another thread).
For our problem we will use two events video_ready
and audio_ready
which will enable our two threads to scream at each other “I am ready ! Are you ?”. Here is the Python fot that:
1 |
|
and finally the code for view(movie)
:
1 |
|
A few tips tips to go further:
- Here I am using the module
threading
, and the two threads will be played in parrallel on the same processor. If you have a computer with several processors you can also use themultiprocessing
module to have your threads played on two different processors (which can be MUCH faster). Nicely enough the two modules have the same syntax: simply replacethreading
bymultiprocessing
andThread
byProcess
in the example above and it should work. - In my original program, I also use an Event to terminate
play_video
andplay_audio
at the same time: when the video playing is exited,play_video
unsets that Event. Inplay_audio
, this event is regularly checked, and when it is seen to be unset,play_audio
exits too. - Instead of using
wait
to wait for an Event to be set, you can use a loop to you decide at which frequency you want to check the Event. Only do that if don’t mind a lag of a few milliseconds between your processes :
1 |
|
Posted by Zulko Sep 19th, 2013 MoviePy, Python,, threads,
A Basic Example of Threads Synchronization in Python, python中的线程同步示例的更多相关文章
- Python程序中的线程操作(线程池)-concurrent模块
目录 Python程序中的线程操作(线程池)-concurrent模块 一.Python标准模块--concurrent.futures 二.介绍 三.基本方法 四.ProcessPoolExecut ...
- Python并发编程-进程 线程 同步锁 线程死锁和递归锁
进程是最小的资源单位,线程是最小的执行单位 一.进程 进程:就是一个程序在一个数据集上的一次动态执行过程. 进程由三部分组成: 1.程序:我们编写的程序用来描述进程要完成哪些功能以及如何完成 2.数据 ...
- 30、Python程序中的线程操作(oncurrent模块)
进程是cpu资源分配的最小单元,一个进程中可以有多个线程. 线程是cpu计算的最小单元. 对于Python来说他的进程和线程和其他语言有差异,是有GIL锁. GIL锁 GIL锁保证一个进程中同一时刻只 ...
- Python程序中的线程操作-锁
目录 一.同步锁 1.1 多个线程抢占资源的情况 1.1.1 对公共数据的操作 1.2 同步锁的引用 1.3 互斥锁与join的区别 二.死锁与递归锁 2.1 死锁 2.2 递归锁RLock 三.典型 ...
- Python程序中的线程操作-concurrent模块
目录 一.Python标准模块--concurrent.futures 二.介绍 三.基本方法 四.ProcessPoolExecutor 五.ThreadPoolExecutor 六.map的用法 ...
- Python程序中的线程操作-创建多线程
目录 一.python线程模块的选择 二.threading模块 三.通过threading.Thread类创建线程 3.1 创建线程的方式一 3.2 创建线程的方式二 四.多线程与多进程 4.1 p ...
- Python程序中的线程操作-线程队列
目录 一.线程队列 二.先进先出 三.后进先出 四.存储数据时可设置优先级的队列 4.1 优先级队列 4.2 更多方法说明 一.线程队列 queue队列:使用import queue,用法与进程Que ...
- [b0032] python 归纳 (十七)_线程同步_信号量Semaphore
代码: # -*- coding: utf-8 -*- """ 多线程并发同步 ,使用信号量threading.Semaphore 逻辑: 多个线程,对同一个共享变量 , ...
- Python程序中的线程操作-守护线程
目录 一.守护线程 1.1 详细解释 1.2 守护线程例1 1.3 守护线程例2 一.守护线程 无论是进程还是线程,都遵循:守护xx会等待主xx运行完毕后被销毁.需要强调的是:运行完毕并非终止运行. ...
随机推荐
- bootstrap大图轮播手机端不能手指滑动解决办法
网上看了很多解决办法,几乎本质都是一样的,都是引入一个滑动的js插件,加入一段js代码,即可生效,但是我试了hammer.js 和 touchSwipe.js 都不生效,也找不到原因是什么,目前在网上 ...
- 【Matlab】运动目标检测之“帧差法”
videoObj = VideoReader('4.avi');%读视频文件 nframes = get(videoObj, 'NumberOfFrames');%获取视频文件帧个数 : nframe ...
- Python 处理命令行参数
optparse模块用于从命令行直接读取参数,用法基本与 argparse模块 一致,如下: #!/usr/bin/env python #-*- coding:utf-8 -*- from optp ...
- K-mean和k-mean++
(1)k-mean聚类 k-mean聚类比较容易理解就是一个计算距离,找中心点,计算距离,找中心点反复迭代的过程, 给定样本集D={x1,x2,...,xm},k均值算法针对聚类所得簇划分C={C1, ...
- Vscode 修改为中文语言
1 官网下载最新版的vscode : https://code.visualstudio.com/Download 2 安装之后, 按键 F1 搜索框 输入 language 选择 config ...
- MUI 二维码扫描并跳转
1 首页 index.html <li id="html/barcode.html" onclick="clicked(this.id)"> < ...
- MQTT的学习研究(十四) MQTT moquette 的 Callback API 消息发布订阅的实现
在moquette-mqtt中提供了回调callback模式的发布和订阅但是在订阅之后没有发现有消息接收的方法,参看moquette-mqtt中Block,Future式的发布订阅基础是callbac ...
- 聊聊对APM的理解
本文主要从以下几个列举对APM的认识: -什么是APM工具 -为什么要用APM工具,APM工具的价值在哪里: -什么样的APM工具适合于传统金融业: -如何用好APM工具: -精准告警 - ...
- 【BZOJ5110】[CodePlus2017]Yazid 的新生舞会 线段树
[BZOJ5110][CodePlus2017]Yazid 的新生舞会 Description Yazid有一个长度为n的序列A,下标从1至n.显然地,这个序列共有n(n+1)/2个子区间.对于任意一 ...
- 【BZOJ1801】[Ahoi2009]chess 中国象棋 DP
[BZOJ1801][Ahoi2009]chess 中国象棋 Description 在N行M列的棋盘上,放若干个炮可以是0个,使得没有任何一个炮可以攻击另一个炮. 请问有多少种放置方法,中国像棋中炮 ...