Introduction: This series will cover techniques related to Python parallel programming including Python threads, Python processes, asynchronous mode of concurrent programming and techniques related to ultimate Python distributed calculations like Celery, SCOOP etc.

Threading multiprocessing asyncio tasks


Basic concepts of threads and two ways to define threads

What is a thread?

Threads look like lightweight processes, but what is a process? Process means that we usually run the program, such as the browser opened by clicking the icon, QQ is a process, process has its own independent memory space address, can have multiple threads; That is, threads exist in the process, which means that threads within a process can share some resources, and the switch between threads is much lower than the process. Multiple threads can execute in parallel and concurrently, sharing data and resources. Therefore, most of our thread communication is using the space of shared information to communicate. This is an essential way for thread management to communicate with multiple threads.

How are threads defined in Python?

The simplest way to use threads is through instantiation of the object function:

import threading
import time


Use to instantiate thread object functions
def function(i):
    time.sleep(2)
    print('Thread {} is running.'.format(i))
    time.sleep(2)
    return


for i in range(5) :# Python module threading.thread method
    t = threading.Thread(target=function, args=(i, ))
    t.start()
Copy the code

The operation screenshot is as follows:

target
args
Tuples tuple
start()

Implement a new thread using the thread module:

import threading
import time


class myThread(threading.Thread):
    def __init__(self, i):
        threading.Thread.__init__(self)
        self.i = i

    def run(self):
        time.sleep(2)
        print('Thread {} is running.'.format(self.i))
        time.sleep(2)
        return


for i in range(1.6):
    t = myThread(i)
    t.start()
Copy the code

The operation screenshot is as follows:

__init__
run()
start()

Daemon thread setDaemon(True)

Next we are running a code:

import threading
import time


class myThread(threading.Thread):
    def __init__(self, i):
        threading.Thread.__init__(self)
        self.i = i

    def run(self):
        time.sleep(2)
        print('Thread {} is running.'.format(self.i))
        time.sleep(2)
        return

print('Mian THread starting')

for i in range(1.6):
    t = myThread(i)
    t.start()
    
print('Mian THread end')
Copy the code

The operation screenshot is as follows:

print()

import threading
import time


class myThread(threading.Thread):
    def __init__(self, i):
        threading.Thread.__init__(self)
        self.i = i

    def run(self):
        time.sleep(2)
        print('Thread {} is running.'.format(self.i))
        time.sleep(2)
        return

print('Mian THread starting')

for i in range(1.6):
    t = myThread(i)
    t.setDaemon(True)
    t.start()
    
print('Mian THread end')
Copy the code

The operation screenshot is as follows:

Blocking thread join()

import threading
import time


class myThread(threading.Thread):
    def __init__(self, i):
        threading.Thread.__init__(self)
        self.i = i

    def run(self):
        time.sleep(2)
        print('Thread {} is running.'.format(self.i))
        time.sleep(2)
        return


print('Mian THread starting')

threads = []
for i in range(1.6):
    t = myThread(i)
    t.start()
    threads.append(t)

for t in threads:
    t.join()
    
print('Mian THread end')

Copy the code

The operation screenshot is as follows:

join()