Python multithreading and processes

threading use (industrial wind case)

import threading
from time import sleep, ctime


loop = [4, 2]


class ThreadFunc:

    def __init__(self, name):
        self.name = name

    def loop(self, nloop, nsec):
        '''
        :param nloop: loop The name of the function
        :param nsec: System sleep time
        :return:
        '''
        print('Start loop ', nloop, 'at ', ctime())
        sleep(nsec)
        print('Done loop ', nloop, ' at ', ctime())


def main():
    print("Starting at: ", ctime())

    # ThreadFunc("loop").loop is the same as the following two formulas:
    # t = ThreadFunc("loop")
    # t.loop
    # t1 and t2 are defined in the same way
    t = ThreadFunc("loop")
    t1 = threading.Thread( target = t.loop, args=("LOOP1", 4))
    # The following way is more western and industrialized
    t2 = threading.Thread( target = ThreadFunc('loop').loop, args=("LOOP2", 2))

    # Common mistakes
    #t1 = threading.Thread(target=ThreadFunc('loop').loop(100,4))
    #t2 = threading.Thread(target=ThreadFunc('loop').loop(100,2))

    t1.start()
    t2.start()

    t1.join( )
    t2.join()

    print("ALL done at: ", ctime())


if __name__ == '__main__':
    main()

Output >

Starting at: Sun Sep 2 10:04:47 2018
Start loop LOOP1 at Sun Sep 2 10:04:47 2018
Start loop LOOP2 at Sun Sep 2 10:04:47 2018
Done loop LOOP2 at Sun Sep 2 10:04:49 2018
Done loop LOOP1 at Sun Sep 2 10:04:51 2018
ALL done at: Sun Sep 2 10:04:51 2018

Analysis: Note: during instantiation, the threading.Thread(target=xxx, args=(xxx,)) format is complete. The industrial style writing method is the function of class init, and args is the rest param, which can be done in one line

  1. You can set the daemons to end the unimportant threads together with the main thread

    t1 = threading.Thread(target=fun, args=() )
    # The method of social Guardian thread must be set before start, otherwise it is invalid
    t1.setDaemon(True)
    #t1.daemon = True
    t1.start()
  2. The two threads of threading.Lock(), both of which compete for resource acquire(), make it impossible to release(), and finally cannot continue the program.

  3. threading.Semaphore(n)n = number of threads allowed to run simultaneously

  4. threading.Timer(t, func) specifies the time to start the thread

Multi process

  1. Using multiprocessing.Process() to generate process directly

  2. Create subclass generation:

    import multiprocessing
    from time import sleep, ctime
    
    
    class ClockProcess(multiprocessing.Process):
        '''
        //Two functions are more important
        1. init Constructor
        2. run
        '''
    
        def __init__(self, interval):
            super().__init__()
            self.interval = interval
    
        def run(self):
            while True:
                print("The time is %s" % ctime())
                sleep(self.interval)
    
    
    if __name__ == '__main__':
        p = ClockProcess(3)
        p.start()
    
        while True:
            print('sleeping.......')
            sleep(1)
    

    Be careful:

    1. __In init, use super()__

    2. Rewrite run()

    3. You can use os.getppid() to get the parent process id, and os.getpid() to get the process id

    4. Establishment process:

      q = multiprocessing.JoinableQueue()
      # Run consumer process
      cons_p = multiprocessing.Process (target = consumer, args = (q,))
      cons_p.daemon = True
      cons_p.start()

example:

import multiprocessing
from time import ctime


def consumer(input_q):
    print("Into consumer:", ctime())
    while True:
        item = input_q.get()
        if item is None:
            break
        print("pull", item, "out of q")
    print("Out of consumer:", ctime())


def producer(sequence, output_q):
    for item in sequence:
        print("Into procuder:", ctime())
        output_q.put(item)
        print("Out of procuder:", ctime())


if __name__ == '__main__':
    q = multiprocessing.Queue()
    cons_p1 = multiprocessing.Process(target=consumer, args=(q,))
    cons_p1.start()

    cons_p2 = multiprocessing.Process (target=consumer, args=(q,))
    cons_p2.start()

    sequence = [1, 2, 3, 4]
    producer(sequence, q)

    q.put(None)
    q.put(None)

    cons_p1.join()
    cons_p2.join()

Into procuder: Tue Sep 4 15:57:37 2018
Out of procuder: Tue Sep 4 15:57:37 2018
Into procuder: Tue Sep 4 15:57:37 2018
Out of procuder: Tue Sep 4 15:57:37 2018
Into procuder: Tue Sep 4 15:57:37 2018
Out of procuder: Tue Sep 4 15:57:37 2018
Into procuder: Tue Sep 4 15:57:37 2018
Out of procuder: Tue Sep 4 15:57:37 2018
Into consumer: Tue Sep 4 15:57:37 2018
pull 1 out of q
pull 2 out of q
pull 3 out of q
pull 4 out of q
Out of consumer: Tue Sep 4 15:57:37 2018
Into consumer: Tue Sep 4 15:57:37 2018
Out of consumer: Tue Sep 4 15:57:37 2018

Analysis:

  1. multiprocessing.Queue() creates an inter process queue
  2. Queue.put() and Queue.get() are queue operations, first in, first out

Keywords: Python REST

Added by supernova on Mon, 06 Jan 2020 12:17:29 +0200