Skip to content

Commit 7d370d2

Browse files
committed
add daemon thread tutorial
1 parent ce92525 commit 7d370d2

File tree

4 files changed

+35
-0
lines changed

4 files changed

+35
-0
lines changed

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
121121
-[Logging in Python](https://www.thepythoncode.com/article/logging-in-python). ([code](python-standard-library/logging))
122122
-[How to Make a Chat Application in Python](https://www.thepythoncode.com/article/make-a-chat-room-application-in-python). ([code](python-standard-library/chat-application))
123123
-[How to Delete Emails in Python](https://www.thepythoncode.com/article/deleting-emails-in-python). ([code](python-standard-library/deleting-emails))
124+
-[Daemon Threads in Python](https://www.thepythoncode.com/article/daemon-threads-in-python). ([code](python-standard-library/daemon-thread))
124125

125126
-### [Using APIs](https://www.thepythoncode.com/topic/using-apis-in-python)
126127
-[How to Automate your VPS or Dedicated Server Management in Python](https://www.thepythoncode.com/article/automate-veesp-server-management-in-python). ([code](general/automating-server-management))
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# [Daemon Threads in Python](https://www.thepythoncode.com/article/daemon-threads-in-python)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
importthreading
2+
importtime
3+
4+
deffunc_1():
5+
whileTrue:
6+
print(f"[{threading.current_thread().name}] Printing this message every 2 seconds")
7+
time.sleep(2)
8+
9+
# initiate the thread with daemon set to True
10+
daemon_thread=threading.Thread(target=func_1, name="daemon-thread", daemon=True)
11+
# or
12+
# daemon_thread.daemon = True
13+
# or
14+
# daemon_thread.setDaemon(True)
15+
daemon_thread.start()
16+
# sleep for 10 seconds and end the main thread
17+
time.sleep(4)
18+
# the main thread ends
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
importthreading
2+
importtime
3+
4+
deffunc():
5+
whileTrue:
6+
print(f"[{threading.current_thread().name}] Printing this message every 2 seconds")
7+
time.sleep(2)
8+
9+
# initiate the thread to call the above function
10+
normal_thread=threading.Thread(target=func, name="normal_thread")
11+
# start the thread
12+
normal_thread.start()
13+
# sleep for 4 seconds and end the main thread
14+
time.sleep(4)
15+
# the main thread ends

0 commit comments

Comments
(0)