多进程
import multiprocessing,time
def run(name):
time.sleep(2)
print("hello",name)
if __name__=='__main__':
for i in range(10):
p=multiprocessing.Process(target=run,args=("xiaohu %s"%i,))
p.start()
#p.join()
进程包含线程
import multiprocessing,time
import threading
def thread_run():
print(threading.get_ident())#线程
def run(name):#进程包含线程
time.sleep(2)
print("hello",name)
t=threading.Thread(target=thread_run,)
t.start()
if __name__=='__main__':
for i in range(10):
p=multiprocessing.Process(target=run,args=("xiaohu %s"%i,))
p.start()
#p.join()
每一个进程都是由父进程启动的
from multiprocessing import Process
import os
def info(title):#通过python进程运行,所以主进程是pythcharm
print(title)
print('module name:', __name__)
print('parent process:', os.getppid())#打印主进程id
print('process id:', os.getpid())#子进程id
print("
")
def f(name):
info(' 33[31;1mfunction f 33[0m')#通过info运行,主进程是info
print('hello', name)#子进程
if __name__ == '__main__':
info(' 33[32;1mmain process line 33[0m')
p = Process(target=f, args=('bob',))
p.start()
p.join()
两个进程是独立的,默认是不能访问的,但是只能通过中间件去访问
Queues 队列
实现多进程之间通信
from multiprocessing import Process,Queue
def f(qq):
qq.put([42,None,'hello'])
if __name__=='__main__':
q=Queue()#父进程
p=Process(target=f,args=(q,))#子进程
p.start()
print(q.get())
q=qq 克隆
conn.close()
if __name__ == '__main__':
parent_conn, child_conn = Pipe()
p = Process(target=f, args=(child_conn,))
p.start()
print(parent_conn.recv()) # prints "[42, None, 'hello']"
p.join()
pip
from multiprocessing import Process, Pipe
def f(conn):
conn.send([42, None, 'hello'])
刚刚只是实现了数据传递,现在实现数据共享
manger
进程锁 为了防止屏幕共享锁定
进程池
协程
greenlet 手动切换携程
from greenlet import greenlet
def run():
print("22")
gr2.switch()
print("45")
gr2.switch()
def bar():
print("33")
gr1.switch()
print("56")
gr1=greenlet(run)
gr2=greenlet(bar)
gr1.switch()
gevent 自动切换 遇到IO就切换
import gevent
def foo():
print('Running in foo')
gevent.sleep(0)
print('Explicit context switch to foo again')
def bar():
print('Explicit context to bar')
gevent.sleep(0)
print('Implicit context switch back to bar')
gevent.joinall([
gevent.spawn(foo),
gevent.spawn(bar),
])
gevent实现单线程socket并发
import sys
import socket
import time
import gevent
from gevent import socket,monkey
monkey.patch_all()
def server(port):
s = socket.socket()
s.bind(('0.0.0.0', port))
s.listen(500)
while True:
cli, addr = s.accept()
gevent.spawn(handle_request, cli)
def handle_request(conn):
try:
while True:
data = conn.recv(1024)
print("recv:", data)
conn.send(data)
if not data:
conn.shutdown(socket.SHUT_WR)
except Exception as ex:
print(ex)
finally:
conn.close()
if __name__ == '__main__':
server(8001)
import socket
HOST = 'localhost' # The remote host
PORT = 8001 # The same port as used by the server
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
while True:
msg = bytes(input(">>:"),encoding="utf8")
s.sendall(msg)
data = s.recv(1024)
#print(data)
print('Received', repr(data))
s.close()
论事件驱动与异步IO