Python Intermediate Tutorial #5 - Events and Daemon Threads
Skills:
AI Pair Programming80%
In today's episode, we are talking about events and daemon threads!
Website: https://www.neuralnine.com/
Instagram: https://www.instagram.com/neuralnine
Twitter: https://twitter.com/neuralnine
GitHub: https://github.com/NeuralNine
Programming Books: https://www.neuralnine.com/books/
Outro Music From: https://www.bensound.com/
Subscribe and Like for more free content!
What You'll Learn
The video covers Python intermediate concepts, specifically events and daemon threads, using the Python programming language and its threading module.
Full Transcript
what is going on guys welcome to this Python tutorial series for intermediates in today's episode number five we're continuing to talk about threading so today we're going to learn about events and daemon threats two very interesting elements when it comes to threading so let us get into the code now events are basically just things in Python that we can trigger and when we trigger them we can react to them in the code so to work with events we again need to import the threading module and I increase the font size a little bit more so every one of you can see that if it gets too big tell me I'm going to adjust it or based on your comments so to create an event in Python we just have to say event or call it my event doesn't matter event equals threading dot event with a capital e now this right here is our event and we can trigger this event and we can wait for this event so we can it is just basic basically an element an object that has the function to be triggered and when we trigger it we can make certain things happen so for example let's define a function my function here which just waits for the event to trigger so print waiting for event to trigger and then we call the weight function of the events so we say you vent dot weight and this makes our function wait until the event is triggered so it will be stuck there until our event is triggered and after the event is triggered we can just do whatever we want to do so here would be the reaction of the event being triggered so I can say our performing action XYZ now and basically this would be the reaction or the respond to the event being triggered now what I can do next is I can just create a thread or that's just called t1 equals threading dot threat with a capital T again and to find this function to be the target function and then starting the thread so what captain's here is we run a thread t1 which just waits for this event to be triggered and until it's triggered nothing happens so what we can then do is we can tell the user to input let's just call it access input do you want the or do you want to trigger the event question mark and then we can specify yes/no and then I can check if X equals y I can trigger the event by calling the set method so I set the event which means I triggered you an event I just say okay now you can stop waiting because the event is activated and else or let's say L if X equals or actually you can say else doesn't matter if anything but Y gets input or put in we just actually just we can just not use that l street here so when I run this right now I see waiting for event to trigger and immediately after that I print do you want to trade you event so maybe I should let's do that maybe I should use a line break here because it gets printed almost simultaneously still it doesn't work oh this time their printers first let's also enter one here now it works so we see waiting for event to trigger and we can now choose to trigger the event or not now notice that in the background where is them like the main thread is still waiting for our input but the other fret T one is still running so it's not waiting because we have to input something it's waiting because the the event is waiting we call the event function so basically nothing happens until we say yes and then the event gets triggered by using the set function and we are performing some action right now so we can use this concept here in much more sophisticated ways but this is how it basically works now let us talk about daemon threats daemon threats are very interesting because they are running in the background and the script terminates even though they're still running because they're not vital to the program so if we have a couple of threads running the program usually waits for all these threats to finish to be stopped but for daemon threats nobody waits for daemon threats they just the script just terminates it doesn't matter if daemon threats are still running because they're running in the background and they're not vital to the program so we could use a daemon --thread for reading constantly reading in information from a file constantly reading in information from a web api or something and as soon as everything else is done we just terminate the script and we also termed a daemon --thread so we're going to look in an example here let's just import threading and import time and what we're going to do is we're going to have two threats one threat is a daemon --thread and it will be reading in information from a text file whereas the other threat is going to print that information out onto the screen so it will be a print loop and the other one would be just reading in in the background reading in information from that file so first of all let us specify a file text dot txt and then we have an empty string here which is just the string or the text to be printed out on the screen and now we're going to define a function read text or let's call it real read file and this function will do a constant loop will have a or will run a constant loop that reads in all the information from the file and waiting three seconds so we're going to say global paths are and global text and we're going to say while true so an endless loop while true we're going to use a with statement here while true Open Text dot txt in Reading Mode S F and then we're going to say text equals F dot read and of course we're going to wait after that for three seconds so this is basically our read file method it's in an endless loop and this anti loop always reads or scans in the information from text file wait three seconds and does the same thing again so the second function would be the print loop this print loop is just going to actually I don't think that we need global text here because we're not changing anything so we're going to say for X in our range 30 so that sometimes hopefully it terminates we're going to say 30 times we're going to print what we have in the text are in the text variable so we're going to say just print text and then we're going to wait for one second every time and what we're going to do now is we're going to run this function here read file in a daemon --thread so it's not important so even though it's an endless loop we can just terminate the loop without terminate at terminating it manually but when this threat here finishes we're just going to say okay we're done and terminate the script so what we're going to do is we're going to say t1 equals threading dot threat and the target function is read file and this threat is a daemon --thread so we're going to specify daemon equals true and t2 is threading dot threat target equals print loop and now we're going to just start t1 and t2 but we're going to not print a 30 times because we would have to wait too long I let's just say 10 times and of course I have to create the file first or actually let's do 30 times because we need some time to change the information in the file again I need to create a file so let's just say text on txt and here I'm going to say hello world and close the file and now I'm going to run my script here and you'll see that it prints hello world all the time and now I can change it to hey guys and save it and you'll see that my daemon --thread is constantly reading in the information now I can say hey then I can say B and you can see that even though I changed B it printed a one more time because it needs three seconds just kind of fall again so basically it will print this now for 30 times and then it will say I'm done and we're going to terminate the script because the daemon --thread is no longer important so actually I didn't terminate the script right now but it terminates itself it just has some buck with me or it has a bug here because oftentimes when I use when I'm using threading it doesn't show that the script is terminated but the basically terminated the script because the print loop or the second threat ug2 was done and t1 was just a daemon --thread so not important and it terminated the script we were done so that's basically everything that you need to know about daemon threats and events I hope you learned something I hope you enjoyed the video if so hit the like button and subscribe to this channel if you want to see more feel free to give feedback and ask questions in the comment section down below and thank you very much for watching see you in the next video bye [Music]
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from NeuralNine · NeuralNine · 16 of 60
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
▶
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Visualizing Stock Data With Candlestick Charts in Python
NeuralNine
Python Beginner Tutorial #1 - Installation and First Program
NeuralNine
Python Beginner Tutorial #2 - Variables and Data Types
NeuralNine
Python Beginner Tutorial #3 - Operators and User Input
NeuralNine
Python Beginner Tutorial #4 - If Statements and Conditions
NeuralNine
Python Beginner Tutorial #5 - Loops
NeuralNine
Python Beginner Tutorial #6 - Sequences and Collections
NeuralNine
Python Beginner Tutorial #7 - Functions
NeuralNine
Python Beginner Tutorial #8 - Exception Handling
NeuralNine
Python Beginner Tutorial #9 - File Operations
NeuralNine
Python Beginner Tutorial #10 - String Functions
NeuralNine
Python Intermediate Tutorial #1 - Classes and Objects
NeuralNine
Python Intermediate Tutorial #2 - Inheritance
NeuralNine
Python Intermediate Tutorial #3 - Multithreading
NeuralNine
Python Intermediate Tutorial #4 - Synchronizing Threads
NeuralNine
Python Intermediate Tutorial #5 - Events and Daemon Threads
NeuralNine
Python Intermediate Tutorial #6 - Queues
NeuralNine
Python Intermediate Tutorial #7 - Sockets and Network Programming
NeuralNine
Python Intermediate Tutorial #8 - Database Programming
NeuralNine
Python Intermediate Tutorial #9 - Recursion
NeuralNine
Python Intermediate Tutorial #10 - XML Processing
NeuralNine
Python Intermediate Tutorial #11 - Logging
NeuralNine
Python Data Science Tutorial #1 - Anaconda and PyCharm Setup
NeuralNine
Python Data Science Tutorial #2 - NumPy Arrays
NeuralNine
Python Data Science Tutorial #3 - Numpy Functions
NeuralNine
Python Data Science Tutorial #4 - Plotting Functions With Matplotlib
NeuralNine
Python Data Science Tutorial #5 - Subplots and Multiple Windows
NeuralNine
Python Data Science Tutorial #6 - Matplotlib Styling
NeuralNine
Python Data Science Tutorial #7 - Bar Charts with Matplotlib
NeuralNine
Python Data Science Tutorial #8 - Pie Charts with Matplotlib
NeuralNine
Python Data Science Tutorial #9 - Plotting Histograms with Matplotlib
NeuralNine
Python Data Science Tutorial #10 - Scatter Plots with Matplotlib
NeuralNine
Python Data Science Tutorial #11 - 3D Plotting with Matplotlib
NeuralNine
Python Data Science Tutorial #12 - Pandas Series
NeuralNine
Python Data Science Tutorial #13 - Pandas Data Frames
NeuralNine
Python Data Science Tutorial #14 - Pandas Statistics
NeuralNine
Python Data Science Tutorial #15 - Pandas Sorting and Functions
NeuralNine
Python Data Science Tutorial #16 - Pandas Merging Data Frames
NeuralNine
Python Data Science Tutorial #17 - Pandas Queries
NeuralNine
Python Machine Learning Tutorial #1 - What is Machine Learning?
NeuralNine
Python Machine Learning Tutorial #2 - Linear Regression
NeuralNine
Python Machine Learning Tutorial #3 - K-Nearest Neighbors Classification
NeuralNine
Python Machine Learning #4 - Support Vector Machines
NeuralNine
Python Machine Learning Tutorial #5 - Decision Trees and Random Forest Classification
NeuralNine
Python Machine Learning Tutorial #6 - K-Means Clustering
NeuralNine
Python Machine Learning Tutorial #7 - Neural Networks
NeuralNine
Python Machine Learning Tutorial #8 - Handwritten Digit Recognition with Tensorflow
NeuralNine
Generating Poetic Texts with Recurrent Neural Networks in Python
NeuralNine
Stock Portfolio Visualization with Matplotlib in Python
NeuralNine
Analyzing Coronavirus with Python (COVID-19)
NeuralNine
Making Text Images Readable Again with Python and OpenCV
NeuralNine
Neural Networks Simply Explained (Theory)
NeuralNine
Motion Filtering with OpenCV in Python
NeuralNine
Top 5 Programming Languages To Learn in 2020
NeuralNine
Simple TCP Chat Room in Python
NeuralNine
Image Classification with Neural Networks in Python
NeuralNine
Edge Detection with OpenCV in Python
NeuralNine
S&P 500 Web Scraping with Python
NeuralNine
Simple Sentiment Text Analysis in Python
NeuralNine
Introduction - Algorithms & Data Structures #1
NeuralNine
More on: AI Pair Programming
View skill →Related AI Lessons
🎓
Tutor Explanation
DeepCamp AI