A Basic Example of Threads Synchronization in Python, python中的线程同步示例
2017-09-15 22:21
621 查看
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:
In this code,
To have a well-synchronized movie we need the internal functions
The solution seems to be using
For our problem we will use two events
and finally the code for
A few tips tips to go further:
Here I am using the module
In my original program, I also use an Event to terminate
Instead of using
Posted by Zulko Sep 19th, 2013 MoviePy, Python,, threads,
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 2 3 4 | def view(movie): new_thread( play_audio( movie ) ) play_video( movie ) |
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 2 3 45 | def play_audio(movie): audio = prepare_audio( movie ) audio.start_playing() def play_video(movie): video = prepare_video( movie ) video.start_playing() |
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.Eventobjects. An
Eventis 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_readyand
audio_readywhich will enable our two threads to scream at each other “I am ready ! Are you ?”. Here is the Python fot that:
1 2 3 45 | import threading def play_audio(movie, audio_ready, video_ready): audio = prepare_audio( movie ) audio_ready.set() # Say "I'm ready" to play_video() video_ready.wait() # Wait for play_video() to say "I'm ready" audio.start_playing() def play_video(movie, audio_ready, video_ready): video = prepare_video( movie ) video_ready.set() # Say "I'm ready" to play_audio() audio_ready.wait() # Wait for play_audio() to say "I'm ready" video.start_playing() |
view(movie):
1 2 3 45 | def view(movie): audio_ready = threading.Event() video_ready = threading.Event() # launch the parrallel audio thread audiothread = threading.Thread(target=play_audio, args = (movie, audio_ready, video_ready)) audiothread.start() play_video(movie, audio_ready, video_ready) |
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 the
multiprocessingmodule to have your threads played on two different processors (which can be MUCH faster). Nicely enough the two modules have the same syntax: simply replace
threadingby
multiprocessingand
Threadby
Processin the example above and it should work.
In my original program, I also use an Event to terminate
play_videoand
play_audioat the same time: when the video playing is exited,
play_videounsets that Event. In
play_audio, this event is regularly checked, and when it is seen to be unset,
play_audioexits too.
Instead of using
waitto 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 2 3 | import time while not audio_ready.is_set(): time.sleep(0.002) # sleep 2 milliseconds |
相关文章推荐
- Python example of SSH in multi-threads
- A example code of multithread communication in Python
- example of python operator overloadind
- the basic operation of sequence in python
- How Synchronization works in Java ? Example of synchronized Block and Method
- Rails Web App Learning in action (2)--the basic version of students selective courses
- Python <built-in method items of dict object at 0x0000000002B6C8C8> 问题解决
- An Example Of Validating Text Item In Oracle Forms Using When-Validate-Item Trigger
- An example usage of an encryption algorithm (AES, in this case) is:
- Example of util:properties in Spring
- Example of Unix commands implemented in Java
- The study of generator in Python(20170912)
- The study of iterator in Python(20170912)
- Example of UUID generation in c++
- A bug in example of JS definitive guide 6th
- Basic OOP in Python
- 【MongoDB】The basic operation of Index in MongoDB
- An Example of Pre-Query and Post-Query Triggers in Oracle Forms With Using Display_Item to Highlight Dynamically
- How to install and manage different versions of Python in Linux
- How to Get a List of Class Attributes in Python