-
In Python programs, threads can be used in two ways: using functions or using classes to wrap thread objects. When using the thread module to process threads, you can call the function start_new_thread () to generate a new thread.
-
1 _thread.start_new_thread ( function, args[, kwargs] )
- Where function is a thread function; args represents the parameter passed to the thread function, he must be a tuple type; kwargs is an optional parameter.
-
1 import _thread 2 import time 3 def fun1 (): 4 print ( ' start running fun1 ' ) 5 time.sleep (4 ) 6 print ( ' run fun1 end ' ) 7 def fun2 (): 8 print ( ' start running fun2 ' ) 9 time.sleep (2 ) 10 print ( ' End of run fun2 ' ) 11 if __name__ ==' __Main__ ' : 12 Print ( ' runs ' ) 13 # to create a thread 14 _thread.start_new_thread (fun1, ()) 15 _thread.start_new_thread (fun2, ()) 16 the time.sleep (7)
1 start running 2 start running fun1 3 start running fun2 4 run fun2 end 5 run fun1 end
_thread implementation process
Guess you like
Origin www.cnblogs.com/monsterhy123/p/12682156.html
Recommended
Ranking