1. 程式人生 > >python基礎-執行緒建立、執行緒池、進\執行緒非同步回撥(add_done_callback)、進\執行緒資料共享、ftp執行緒池

python基礎-執行緒建立、執行緒池、進\執行緒非同步回撥(add_done_callback)、進\執行緒資料共享、ftp執行緒池

執行緒建立

程序只是用來把資源集中到一起(程序只是一個資源單位,或者說資源集合),而執行緒才是cpu上的執行單位。
每個程序有一個地址空間,而且預設就有一個控制執行緒
執行緒就是一條流水線工作的過程,一條流水線必須屬於一個車間,一個車間的工作過程是一個程序

多執行緒(即多個控制執行緒)的概念是,在一個程序中存在多個控制執行緒,多個控制執行緒共享該程序的地址空間,相當於一個車間內有多條流水線,都共用一個車間的資源

我們之前瞭解過程序的2種建立方式
下面的程式碼是2種建立執行緒的方式

from threading import Thread
from multiprocessing import
Process import time,os def task(): print('%s is running' %os.getpid()) time.sleep(2) print('%s is done' %os.getpid()) class Mythread(Thread): def __init__(self,name): super().__init__() self.name=name def run(self): print('%s is running' % os.getpid()) time.sleep(5
) print('%s is done' % os.getpid()) if __name__ == '__main__': t=Thread(target=task) # t=Mythread('xxxxx') t.start() print('主')

輸出如下:

E:\python\python_sdk\python.exe "E:/python/py_pro/1 開啟執行緒的兩種方式.py"
10336 is running
主
10336 is done

Process finished with exit code 0

執行緒程序pid

part1:在主程序下開啟多個執行緒,每個執行緒都跟主程序的pid一樣

from threading import Thread
from multiprocessing import Process
import time,os

def task():
    print('partent:%s self:%s' %(os.getppid(),os.getpid()))
    time.sleep(5)

if __name__ == '__main__':
    t=Thread(target=task,)
    # t=Process(target=task,)
    t.start()
    print('主',os.getppid(),os.getpid())

輸出如下:

partent:9052 self101209052 10120

開多個程序,每個程序都有不同的pid

from threading import Thread
from multiprocessing import Process
import time,os

def task():
    print('partent:%s self:%s' %(os.getppid(),os.getpid()))
    time.sleep(5)

if __name__ == '__main__':
    t=Process(target=task,)
    t.start()
    print('主',os.getppid(),os.getpid())

輸出如下:

9052 2668
partent:2668 self8744

執行緒程序資料共享

程序之間資料不共享,但是程序之間可以通過ipc進行資料通訊

from threading import Thread
from multiprocessing import Process
import time,os

n=100
def task():
    global n
    n=0

if __name__ == '__main__':
    t=Process(target=task,)
    t.start()
    t.join()

    print('主',n)

輸出如下:

主 100

執行緒之間記憶體空間共享

from threading import Thread
import time,os

n=100
def task():
    global n
    n=0

if __name__ == '__main__':
    t=Thread(target=task,)
    t.start()
    t.join()

    print('主',n)

輸出如下:

主 0

執行緒ftp

服務端:

import multiprocessing
import threading

import socket
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.bind(('127.0.0.1',8081))
s.listen(5)

def action(conn):
    while True:
        data=conn.recv(1024)
        print(data)
        conn.send(data.upper())

if __name__ == '__main__':

    while True:
        conn,addr=s.accept()

        p=threading.Thread(target=action,args=(conn,))
        p.start()

客戶端:

from socket import *

client=socket(AF_INET,SOCK_STREAM)
client.connect(('127.0.0.1',8081))


while True:
    msg=input('>>: ').strip()
    if not msg:continue

    client.send(msg.encode('utf-8'))
    msg=client.recv(1024)
    print(msg.decode('utf-8'))

執行緒池

from concurrent.futures import ProcessPoolExecutor,ThreadPoolExecutor
from threading import current_thread
import time,random
def task(n):
    print('%s is running' %current_thread().getName())
    time.sleep(random.randint(1,3))
    return n**2

if __name__ == '__main__':

    t=ThreadPoolExecutor(3) #預設是cpu的核數*5
    objs=[]
    for i in range(5):
        obj=t.submit(task,i)
        objs.append(obj)

    t.shutdown(wait=True)
    for obj in objs:
        print(obj.result())
    print('主',current_thread().getName())

輸出如下:

E:\python\python_sdk\python.exe "E:/python/py_pro/4 執行緒池.py"
ThreadPoolExecutor-0_0 is running
ThreadPoolExecutor-0_1 is running
ThreadPoolExecutor-0_2 is running

ThreadPoolExecutor-0_0 is running

ThreadPoolExecutor-0_1 is running

0
1
4
9
16
主 MainThread

執行緒池ftp

服務端:

from socket import *
from concurrent.futures import ThreadPoolExecutor
import os

server=socket(AF_INET,SOCK_STREAM)
server.setsockopt(SOL_SOCKET,SO_REUSEADDR,1)
server.bind(('127.0.0.1',8080))
server.listen(5)

def talk(conn,client_addr):
    print('程序pid: %s' %os.getpid())
    while True:
        try:
            msg=conn.recv(1024)
            if not msg:break
            conn.send(msg.upper())
        except Exception:
            break

if __name__ == '__main__':
    p=ThreadPoolExecutor(5)
    while True:
        conn,client_addr=server.accept()
        p.submit(talk,conn,client_addr)

客戶端:

from socket import *

client=socket(AF_INET,SOCK_STREAM)
client.connect(('127.0.0.1',8081))


while True:
    msg=input('>>: ').strip()
    if not msg:continue

    client.send(msg.encode('utf-8'))
    msg=client.recv(1024)
    print(msg.decode('utf-8'))

執行緒的一些其他方法

from threading import Thread,current_thread,enumerate,active_count
import time,os

def task():
    print('%s is running' %current_thread().getName())
    time.sleep(5)
    print('%s is done' %current_thread().getName())

if __name__ == '__main__':
    t=Thread(target=task,name='xxxx')
    t.start()
    print(t.name)

    #檢視當前活著的執行緒
    print(enumerate()[0].getName())
    print(active_count())
    print('主',current_thread().getName())

print()

輸出如下:

E:\python\python_sdk\python.exe "E:/python/py_pro/3 執行緒物件的其他屬性或方法.py"
xxxx is running
xxxx
MainThread
2
主 MainThread

xxxx is done

非同步-回撥函式

ProcessPoolExecutor方式

我們之前總結的非同步返回結果沒有用到呼叫函式,接下來的是利用了回撥函式

#pip install requests
import requests
from concurrent.futures import ThreadPoolExecutor,ProcessPoolExecutor
from threading import current_thread
import time,os
def get(url):
    print('%s GET %s' %(os.getpid(),url))
    response=requests.get(url)
    time.sleep(3)
    if response.status_code == 200:
        return {'url':url,'text':response.text}

def parse(obj):
    res=obj.result()
    print('[%s] <%s> (%s)' % (os.getpid(), res['url'],len(res['text'])))

if __name__ == '__main__':
    urls = [
        'https://www.python.org',
        'https://www.baidu.com',
        'https://www.jd.com',
        'https://www.tmall.com',
    ]
    t=ProcessPoolExecutor(2)
    for url in urls:
        t.submit(get,url).add_done_callback(parse)
    t.shutdown(wait=True)

    print('主',os.getpid())

程式碼思路是:
t=ProcessPoolExecutor(2)開一個程序池,然後去併發下載網路資料,下載完畢後,
在主程序中add_done_callback去解析
這裡由於主程序、子程序不是同一個程序空間,所以在解析資料時候,在主程序
輸出如下:

E:\python\python_sdk\python.exe "E:/python/py_pro/5 補充非同步的概念.py"
5628 GET https://www.python.org
4816 GET https://www.baidu.com


4816 GET https://www.jd.com
[3204] <https://www.baidu.com> (2443)

[3204] <https://www.python.org> (48856)
5628 GET https://www.tmall.com


[3204] <https://www.jd.com> (124541)


[3204] <https://www.tmall.com> (212080)
主 3204

Process finished with exit code 0

ThreadPoolExecutor方式

import requests
from concurrent.futures import ThreadPoolExecutor,ProcessPoolExecutor
from threading import current_thread
import time
import os
def get(url):
    print('%s GET %s,%s' %(current_thread().getName(),os.getpid(),url))
    response=requests.get(url)
    time.sleep(3)
    if response.status_code == 200:
        return {'url':url,'text':response.text}

def parse(obj):
    res=obj.result()
    print('[%s] <%s> (%s)' % (current_thread().getName(), res['url'],len(res['text'])))

if __name__ == '__main__':
    urls = [
        'https://www.python.org',
        'https://www.baidu.com',
        'https://www.jd.com',
        'https://www.tmall.com',
    ]
    t=ThreadPoolExecutor(2)
    for url in urls:
        t.submit(get,url).add_done_callback(parse)
    t.shutdown(wait=True)

    print('主',current_thread().getName(),os.getpid())

程式碼思路是:
t=ThreadPoolExecutor(2)開一個執行緒池,然後去併發下載網路資料,下載完畢後,
在主執行緒程中add_done_callback去解析
這裡由於主執行緒、子執行緒是同一個程序空間,所以在解析資料時候,可能主執行緒、子執行緒都會解析
輸出如下:

E:\python\python_sdk\python.exe "E:/python/py_pro/5 補充非同步的概念.py"
ThreadPoolExecutor-0_0 GET 12956,https://www.python.org
ThreadPoolExecutor-0_1 GET 12956,https://www.baidu.com

[ThreadPoolExecutor-0_1] <https://www.baidu.com> (2443)
ThreadPoolExecutor-0_1 GET 12956,https://www.jd.com

[ThreadPoolExecutor-0_0] <https://www.python.org> (48856)
ThreadPoolExecutor-0_0 GET 12956,https://www.tmall.com

[ThreadPoolExecutor-0_1] <https://www.jd.com> (124541)

[ThreadPoolExecutor-0_0] <https://www.tmall.com> (212079)
主 MainThread 12956

Process finished with exit code 0