[Python-13/100] processes and threads (Process and Thread)

Copyright: Public Number: Fresh Site. QQ group: 690 274 ​​159. Reprinted with my blog, please attach a reprint address, thank you! ^ _ ^ I'm a hip-hop program ape freshman. https://blog.csdn.net/wuhongxia29/article/details/90762944

Day13 processes and threads


Reference:
1.github-100-days- processes and threads
2. Liao Xuefeng - processes and threads
3.Daemon is not daemon, but what is
it? Recommended tools: Kordsa

concept

  • Process: program, fork, spawn, IPC (pipes, semaphores, sockets, shared memory, etc.)
  • Thread: CPU scheduling execution unit (1-n: Process - thread)

multi-Progress

Single-process and multi-process comparison

# demo1.0
# 普通
from random import randint
from time import time, sleep


def download_task(filename):
    print('开始下载。。。%s' % filename)
    time_to_download = randint(5, 10)
    sleep(time_to_download)
    print('%s下载完成!耗时%d秒' % (filename, time_to_download))

def main():
    start = time()
    download_task('python入门到住院')
    download_task('Peking Hot.avi')
    end = time()
    print('总耗时%.2f' % (end-start))
    
main()

start download. . . Getting to the hospital python
python entry to the hospital the download is complete! It takes five seconds
to start the download. . . Hot.avi Peking
Peking Hot.avi download is complete! It takes five seconds
total time 10.01

# demo1.1
# 多进程
from multiprocessing import Process
from os import getpid
from random import randint
from time import time, sleep


def download_task(filename):
    print('启动下载进程,进程号[%d]' % getpid())
    print('开始下载%s...' % filename)
    time_to_download = randint(5, 10)
    sleep(time_to_download)
    print('%s下载完成,耗时%d秒' % (filename, time_to_download))
    
    
def main():
    start = time()
    p1 = Process(target=download_task, args=('Python入门到住院.pdf', ))
    p1.start()
    p2 = Process(target=download_task, args=('Peking Hot.avi', ))
    p2.start()
    p1.join()
    p2.join()
    end = time()
    print('共耗时%.2f秒' % (end-start))
    
    
main()

Start the download process, process ID [129]
to start downloading Python entry to the hospital .pdf ...
start the download process, process ID [132]
to start downloading Hot.avi ... Peking
Peking Hot.avi the download is complete, takes 6 seconds
Python entry to the hospital. pdf download is complete, it takes seven seconds
took a total of 7.09 seconds

example

We start two processes, one output Ping, Pong one output, the output of the two processes and Ping Pong combined total of 10. It sounds very simple, but if you write wrong but oh.

from multiprocessing import Process
from time import sleep


counter = 0


def sub_task(string):
    global counter
    while counter < 10:
        print(('[%d]'+string) % counter, end='\t', flush=True)  # flush=True,将信息立刻打印(https://blog.csdn.net/u013985241/article/details/86653356)
        
        counter += 1
        sleep(0.01)
    
def main():
    Process(target=sub_task, args= ('Ping', )).start()
    Process(target=sub_task, args= ('Pong', )).start()
        
    
main()

[0]Ping [0]Pong [1]Ping [1]Pong [2]Ping [2]Pong [3]Ping [3]Pong [4]Ping [4]Pong [5]Ping [5]Pong [6]Ping [6]Pong [7]Ping [7]Pong [8]Ping [8]Pong [9]Ping [9]Pong


Multithreading

Example 1

# demo1.0
# 单线程
from random import randint
from threading import Thread
from time import time, sleep


def download(filename):
    print('开始下载%s...' % filename)
    time_to_download = randint(5, 10)
    sleep(time_to_download)
    print('%s下载完成,耗时%.2f秒' % (filename, time_to_download))
    
    
def main():
    start = time()
    t1 = Thread(target=download, args=('Pyton从入门到住院', ))
    t1.start()
    t2 = Thread(target=download, args=('Pyton从入门到精通', ))
    t2.start()
    t1.join()  # 阻塞,主等子。超timeout,主关子(https://blog.csdn.net/zhiyuan_2007/article/details/48807761)
    t2.join()
    end = time()
    print('耗时:%.2f' % (end-start))
    
    
main()

Pyton start downloading from entry to the hospital ...
to start the download Pyton from entry to the master ...
Pyton from entry to the hospital the download is complete, Pyton took 9.00 seconds from entry to the master download is complete, took 9.00 seconds
time: 9.01

Example 2

# 自定义线程类
from random import randint
from threading import Thread
from time import time, sleep


class DownloadTask(Thread):
    
    def __init__(self, filename):
        super().__init__()
        self._filename = filename
        
    def run(self):
        print('开始下载%s...' % self._filename)
        time_to_download = randint(5, 10)
        sleep(time_to_download)
        print('%s下载完成,耗时%.2f秒' % (self._filename, time_to_download))
        

def main():
    start = time()
    t1 = DownloadTask('Python1')
    t1.start()
    t2 = DownloadTask('Python2')
    t2.start()
    t1.join()
    t2.join()
    end = time()
    print('耗时%.2f' % (end-start))
    

main()

Download Python1 start ...
start ... download Python 2
Python 2 download is complete, took 7.00 seconds
Python1 the download is complete, took 8.00 seconds
time-consuming 8.01

Example 3

# 用Lock处理临界资源问题
# GIL(Global Interpreter Lock)(全局解释器锁)
# 每100条字节码(http://python.jobbole.com/56761/),自动释放GIL锁
from time import sleep
from threading import Thread, Lock


class Account(object):
    
    def __init__(self):
        self._balance = 0
        self._lock = Lock()
        
    def deposit(self, money):
        self._lock.acquire()  # 先获取锁才能执行后续代码
        try:
            new_balance = self._balance + money  # 计算存款后的余额
            sleep(0.01)  # 模拟受理存款业务需要0.01秒的时间
            self._balance = new_balance  # 修改账户余额
        finally:
            self._lock.release()  # 保证正常异常锁都能释放
        
    @property
    def balance(self):
        return self._balance
        
        
class AddMoneyThread(Thread):
    
    def __init__(self, account, money):
        super().__init__()
        self._account = account
        self._money = money
        
    def run(self):
        self._account.deposit(self._money)
        

def money():
    account = Account()
    threads = []
    for _ in range(100):  # 创建100个存款的线程向同一个账户存钱
        t = AddMoneyThread(account, 1)
        threads.append(t)
        t.start()
    # 等所有存款线程执行完毕
    for t in threads:
        t.join()
    print('余额:¥%d元' % account.balance)
    

money()    

Balance: ¥ 100 Yuan


Multi-feed / thread selection

many stable Resources speed example
process better high Slower Early Apache server (now mixed [+ line feed])
Thread The poor Faster Early IIS server (now mixed)

Factors

1. Switch

  • Multi-task switching
  • Environmental preservation, restoration

2. Task Types

Types of Total resources speed Feature Be applicable Language example
Computationally intensive many low efficiency Multiple time-consuming task switching multi-Progress C Video codec, format conversion, etc.
I / O dense less Slower than memory, CPU Multithreading Python (Script) Network, the storage medium I / O

+ Threaded asynchronous I / O

Coroutine: + threaded asynchronous I / O
advantages: full use of the multi-core CPU, high efficiency
example: the Nginx server, Node.js server programs developed
max: multi-process coroutine +


stem

  • Python from entry to the hospital / abandon / cervical spondylosis

An error

Type 1 Error: not convert all the parameters in the format string
TypeError: not all arguments converted during string formatting

# 错误
print('开始下载。。。' % filename)

# 正确
print('开始下载。。。%s' % filename)

2. Error type: download_task () gives an argument position, but assigned 14
TypeError: download_task () takes 1 positional argument but 14 were given

# 错误
p2 = Process(target=download_task, args=('Peking Hot.avi'))

# 正确
# 元组只有一个参数时,需要在末尾加','
p2 = Process(target=download_task, args=('Peking Hot.avi', ))

3. Error Type: Descriptor " the init " is a super object requires parameters
TypeError: descriptor ' the init ' of 'super' Object argument AN Needs

# 错误
class AddMoneyThread(Thread):
    
    def __init__(self, account, money):
        super.__init__()
        self._account = account
        self._money = money

# 正确
class AddMoneyThread(Thread):
    
    def __init__(self, account, money):
        super().__init__()
        self._account = account
        self._money = money

English accumulation

1.Because I think the point of this assignment
is you're not supposed to leave threads unlocked. Because I think the focus of this task is that you should not unlock the thread.

Guess you like

Origin blog.csdn.net/wuhongxia29/article/details/90762944