This week began the summer officially ended this week in recognition of the school to arrange internships reason and there is no progress, only to learn what part of the Python thread-related
The first is single-threaded test, run the following figure renderings
code show as below:
. 1 from Time Import SLEEP, the ctime 2 DEF fun1 (): . 3 Print ( ' starts running fun1: ' , the ctime ()) . 4 # Sleep 4S . 5 SLEEP (. 4 ) . 6 Print ( ' fun1 end of run: ' , the ctime ()) . 7 DEF fun2 (): . 8 Print ( ' starts running fun2: ' , the ctime ()) . 9 # sleep 2S 10 sLEEP (2 ) . 11 Print ( ' fun2 end of run:' , The ctime ()) 12 is DEF main (): 13 is Print ( " Start Run time: " , the ctime ()) 14 # call function and fun2 function fun1 single thread 15 fun1 () 16 fun2 () . 17 Print ( " end run time: " , the ctime ()) 18 is IF the __name__ == ' __main__ ' : . 19 main ()
The next test is a multithreaded program, the effect is as follows:
code show as below:
. 1 Import _Thread AS Thread 2 from Time Import SLEEP, the ctime . 3 DEF fun1 (): . 4 Print ( ' starts running fun1: ' , the ctime ()) . 5 # Sleep 4S . 6 SLEEP (. 4 ) . 7 Print ( ' fun1 end of run: ' , the ctime ()) . 8 DEF fun2 (): . 9 Print ( ' starts running fun2: ' , the ctime ()) 10 # sleep 2S . 11 sLEEP (2 ) 12 is Print ( ' fun2 end of run: ' , the ctime ()) 13 is DEF main (): 14 Print ( ' Start Run time: ' , the ctime ()) 15 # start a thread running function fun1 16 thread.start_new_thread (fun1 (), ()) 17 # start a thread running fun2 function 18 is thread.start_new_thread (fun2 (), ()) . 19 # sleep 6S 20 is sLEEP (. 6 ) 21 is Print ( ' end run time: ' , the ctime ()) 22 is IF the __name__ = = ' __main__ ': 23 main()
Is then passed to thread function test parameters, the effect is as follows:
code show as below:
. 1 Import Random 2 from Time Import SLEEP, the ctime . 3 Import _Thread AS Thread . 4 # thread function, wherein a and b are the parameters passed by start_new_thread function . 5 DEF Fun (a, b): . 6 Print (a, b) . 7 # sleep random period of time (1 to 4 seconds) . 8 sLEEP (the random.randint (l, 4 )) . 9 # start 8 threads 10 for I in Range (. 8 ): . 11 # is a two arguments function value for each thread 12 is thread.start_new_thread (Fun, (I +. 1, ' A '* (I +. 1 ))) 13 is # by a string from the input terminal of the program to suspend embodiment 14 INPUT ()
The next test is a thread and a lock, the effect is as follows:
code show as below:
. 1 Import _Thread AS Thread 2 from Time Import SLEEP, the ctime . 3 # thread function, index of an integer type index, sec is the sleep time (unit: second), lock a lock object . 4 DEF Fun (index, sec, Lock): . 5 Print ( ' started ' , index, ' execution time ' , the ctime ()) . 6 # sleep . 7 sLEEP (sec) . 8 Print ( ' execution ends ' , index, ' execution time ' , the ctime ()) . 9 # release lock Object 10 lock.release () 11 DEF main (): 12 # Create the first lock object 13 lock1 = thread.allocate_lock () 14 # acquiring the lock, which is equivalent to engage the lock 15 lock1.acquire () 16 # to start the first thread, and pass The first lock object, the index is 10, the sleep time is 4, is lock1 lock object . 17 thread.start_new_thread (Fun, (10,4 , lock1)) 18 is # create a second lock object . 19 lock2 = thread.allocate_lock () 20 # acquiring the lock, which is equivalent to engage the lock 21 is lock2.acquire () 22 is # start a second thread, the first lock object and pass, the index is 10, the sleep time is 4, is lock1 lock object 23 is thread. start_new_thread (Fun, (20 is, 2 , lock2)) 24 # a while loop and a method of determining whether lock1 locked and released lock2 25 while lock1.locked () or lock2.locked (): 26 is Pass 27 IF the __name__ == ' __main__ ' : 28 main ()
These are all the contents of this week, next week will continue to learn and practice U-NET neural network, come on! ! !