Doing python (v) multi-thread usage

Multithreading similar to the simultaneous execution of multiple different programs, multi-threaded run has the following advantages:

  • The program uses threads can occupy long tasks into the background to deal with.
  • The user interface can be more attractive, such as the user clicks a button to trigger the processing of certain events, you can pop up a progress bar to show the progress of the process.
  • Running speed of the program is likely to accelerate.
  • On some tasks, such as waiting for user input to achieve, document literacy and send and receive network data, the thread is more useful. In this case we can free up some valuable resources such as memory usage and so on.

Each entry has a separate thread running, sequences and procedures performed in the order outlet. But the thread is not able to execute independently, it must exist according to the application, providing multiple execution threads controlled by the application.

Each thread has his own set of CPU registers, called the context of the thread, the thread context reflects the status of the last run of the thread CPU registers.

Instruction pointer and stack pointer registers are the two most important thread context register, the thread always been run in the context of the process, these addresses are used to mark the owning thread of the process address space of memory.

  • Thread can be preempted (interrupted).
  • In other threads are running, the thread can be held in abeyance (also known as sleep) - This is the thread concessions.

Threads can be divided into:

  • Kernel threads: and revoked by the operating system kernel created.
  • User threads: do not need kernel support and thread implementation in the user program.

Python3 common thread in two modules:

  • _thread
  • threading (recommended)

thread module has been abandoned. The user can use instead of threading module. So, you can no longer use the "thread" module in Python3 in. For compatibility, Python3 the thread rename "_thread".

Other methods threading module contains all the methods _thread modules, also provided:

  • threading.currentThread (): Returns the current thread variable.
  • threading.enumerate (): Returns a list of running threads. Refers to the thread starts running, before the end, it does not include a thread before starting and after termination.
  • threading.activeCount (): Returns the number of running threads, and len (threading.enumerate ()) have the same result.

In addition to using the method, the threading module also provides a Thread class processing thread, Thread class provides the following methods:

  • run ():  to indicate the method of active threads.
  • start (): start thread activity.

     

  • join ([time]):  Wait until the thread is suspended. This blocks the calling thread until the thread's join () method is called suspension - normal exit or throw an unhandled exception - or the optional timeout occurs.
  • isAlive ():  Returns the thread is active.
  • getName ():  Returns the thread name.
  • setName ():  Set the thread name.

for example:

 1 #!/usr/bin/python3
 2 # 3birds coypright
 3 
 4 import threading
 5 import time
 6 
 7 exitFlag = 0
 8 
 9 class myThread (threading.Thread):
10     def __init__(self, threadID, name, counter):
11         threading.Thread.__init__(self)
12         self.threadIN = threadID
13         self.name = name
14         self.counter = counter
15     def run(self):
16         print ("3birds start thread:" + self.name)
17         threadLock.acquire()
18         print_time(self.name, self.counter, 5)
19         threadLock.release()
20         print ("3birds exit thread:" + self.name)
21 
22 def print_time(threadName, delay, counter):
23     while counter:
24         if exitFlag:
25             threadName.exit()
26         time.sleep(delay)
27         print ("%s: %s" % (threadName, time.ctime(time.time())))
28         counter -= 1
29 
30 threadLock = threading.Lock()
31 threads = []
32 
33 thread1 = myThread(1, "Thread-1", 1)
34 thread2 = myThread(2, "Thread-2", 2)
35 
36 # start new thread
37 thread1.start()
38 thread2.start()
39 thread1.join()
40 thread2.join()
41 print("end the thread")

  operation result:

3birds start thread:Thread-1
3birds start thread:Thread-2
Thread-1: Tue Jul 23 09:08:46 2019
Thread-1: Tue Jul 23 09:08:47 2019
Thread-1: Tue Jul 23 09:08:48 2019
Thread-1: Tue Jul 23 09:08:49 2019
Thread-1: Tue Jul 23 09:08:50 2019
3birds exit thread:Thread-1
Thread-2: Tue Jul 23 09:08:52 2019
Thread-2: Tue Jul 23 09:08:54 2019
Thread-2: Tue Jul 23 09:08:56 2019
Thread-2: Tue Jul 23 09:08:58 2019
Thread-2: Tue Jul 23 09:09:00 2019
3birds exit thread:Thread-2
end the thread

  Reference documents:

http://www.runoob.com/python3/python3-multithreading.html

Guess you like

Origin www.cnblogs.com/dylancao/p/11229697.html