Py中程序與執行緒

2021-09-27 12:47:00 字數 4537 閱讀 3372

#實現程序

import multiprocessing as mp

def washer(dishes,output):

for dish in dishes:

print('washing',dish,'dish')

output.put(dish)

def dryer(input):

while true:

dish=input.get()

print('drying',dish,'dish')

input.task_done()

dish_queue=mp.joinablequeue()

dryer_proc=mp.process(target=dryer,args=(dish_queue,))

dryer_proc.daemon=true

dryer_proc.start()

dishes=['salad','orange','pear','strawberry']

washer(dishes,dish_queue)

dish_queue.join()

輸出:washing salad dish

washing orange dish

washing pear dish

washing strawberry dish

drying salad dish

drying orange dish

drying pear dish

drying strawberry dish

實現執行緒

import threading

def do_this(what):

whoami(what)

def whoami(what):

print("thread %s says: %s"%(threading.current_thread(),what))

if __name__=='__main__':

whoami("i'm the main threading!")

for n in range(4):

p=threading.thread(target=do_this,args=("i'm function %s"%n,))

p.start()

輸出:thread <_mainthread(mainthread, started 140538584946496)> says: i'm the main threading!

thread says: i'm function 0

thread says: i'm function 1

thread says: i'm function 2

thread says: i'm function 3

重寫前面程序的方法

import threading,queue

import time

def washer(dishes,dish_queue):

for dish in dishes:

print("washing",dish)

time.sleep(5)

dish_queue.put(dish)

def dryer(dish_queue):

while true:

dish=dish_queue.get()

print("drying",dish)

time.sleep(10)

dish_queue.task_done()

dish_queue=queue.queue()

for n in range(2):

dryer_thread=threading.thread(target=dryer,args=(dish_queue,))

dryer_thread.start()

dishes=['salad','bread','entree','desert']

washer(dishes,dish_queue)

dish_queue.join()

輸出:washing salad

washing drying salad

bread

washingdrying bread

entree

washing drying entree

desert

drying desert

import gevent

from gevent import socket

,'www.antique-taxidermy.com']

#gevent會為每個gevent.socket.gethostbyname(url)建立乙個綠色執行緒

jobs=[gevent.spawn(gevent.socket.gethostbyname,host) for host in hosts]

#gevent.joinall會等待所有的任務完成

gevent.joinall(jobs,timeout=5)

for job in jobs:

print(job.value)

輸出:66.6.44.4

104.27.173.75

none

import gevent

#給更多的標準庫打上補丁,直接讓socket使用綠色執行緒而非gevent

from gevent import monkey;monkey.patch_all()

import socket

,'www.antique-taxidermy.com']

#gevent會為每個gevent.socket.gethostbyname(url)建立乙個綠色執行緒

jobs=[gevent.spawn(gevent.socket.gethostbyname,host) for host in hosts]

#gevent.joinall會等待所有的任務完成

gevent.joinall(jobs,timeout=5)

for job in jobs:

print(job.value)

輸出:66.6.44.4

104.27.173.75

none

twisted編寫客戶端以及服務端,注意裡面有坑,就是關於編碼的問題

伺服器端**:

#twisted庫,敲門伺服器以及客戶端

from twisted.internet import protocol,reactor

class knock(protocol.protocol):

def datareceived(self, data):

print('client:',data)

if data.decode().startswith("knock knock"):

response="who is here?"

else:

response=str(data.decode())+"who?"

print("server:",response)

self.transport.write(response.encode())

class knockfactory(protocol.factory):

def buildprotocol(selfself,addr):

return knock()

reactor.listentcp(8000,knockfactory())

reactor.run()

客戶端**:

from twisted.internet import protocol,reactor

class knockclient(protocol.protocol):

def connectionmade(self):

self.transport.write("knock knock".encode())

def datareceived(self, data):

# print(data)

if data.decode().startswith("who is here?"):

self.transport.write(response.encode())

else:

self.transport.loseconnection()

reactor.stop()

class knockfactory(protocol.clientfactory):

protocol=knockclient

if __name__=='__main__':

f=knockfactory()

reactor.connecttcp('localhost',8000,f)

reactor.run()

輸出:

client: b'knock knock'

server: who is here?

py 程序與執行緒

多程序和多執行緒,這是實現多工最常用的兩種方式。首先,要實現多工,通常我們會設計master worker模式,master負責分配任務,worker負責執行任務,因此,多工環境下,通常是乙個master,多個worker。如果用多程序實現master worker,主程序就是master,其他程序...

android中程序和執行緒的概述

在預設的情況下所有的應用的元件都是執行在同乙個程序中的,當然在某種特別耗時的動作中也可以指定新的程序。指定新程序可以通過android process屬性 在系統資源不足時會根據程序級別的不同kill掉執行緒,下面介紹一下程序的級別 乙個activity使用者正在互動 在呼叫onresume方法後 ...

linux執行中程序和執行緒分析

一.首先檢視系統中各程序占用cpu和記憶體的資訊,找出占用資源最多的程序pid。1.用top 獲得程序的動態更新 命令檢視 預設每5秒重新整理一次,按照cpu使用率排行。輸入m可以按照記憶體占用排行。查出占用cpu或記憶體比較高的程序pid。2.用ps 獲得程序的當前快照 命令。查出占用cpu或記憶...