python之进程

 

开启子进程的两种方式:

1、使用Process类

from multiprocessing import Process
import time,os

def f(name):
    print('%s is running,parent id is <%s>'%(os.getpid(),os.getppid()),time.ctime())
    time.sleep(1)
    print('%s is done' % os.getpgid(), time.ctime())

if __name__ == '__main__':
    p_list=[]
    for i in range(3):
        p = Process(target=f, args=('子进程%s'%i,))
        # p = Process(target=f,kwargs={'name':'子进程%s'%i})
        p_list.append(p)
        p.start()  # 仅仅是给操作系统发送了一个信号
    print('主进程',os.getpid())
    for i in p_list:
        p.join()
    print('end')

 

2、写子类继承Process类

from multiprocessing import Process
import time

class MyProcess(Process):
    def __init__(self,name):
        super(MyProcess, self).__init__()
        self.name = name

    def run(self):
        print('%s is running' % self.name, time.ctime())
        time.sleep(1)
        print('%s is done' % self.name, time.ctime())


if __name__ == '__main__':
    p_list=[]
    for i in range(3):
        p = MyProcess('子进程%d'%i)
        p.start()
        p_list.append(p)
    for p in p_list:
        p.join()
    print('end')

 

使用多进程实现并发:

from socket import *
from multiprocessing import Process


def talk(conn):
    while True:
        try:
            data = conn.recv(1024)
            if not data: break
            conn.send(data.upper())
        except ConnectionResetError:
            break
    conn.close()


def server(ip,port): # 来一个客户端,起一个conn
    server = socket(socket.AF_INET, socket.SOCK_STREAM)
    server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    server.bind((ip, port))
    server.listen(5)
    while True:
        conn,addr = server.accept()
        p = Process(target=talk,args=(conn,))  # 响应每个conn
        p.start()
    server.close()

if __name__ == '__main__':
    server('127.0.0.1',8080)

 

守护进程:p.deamon = True       

  1、主进程运行结束后子进程也结束

    2、守护进程里不能再新开子进程

互斥锁:访问同一个文件,同一个打印终端等,把并发改为串行

              与join的区别:join是全局的,block所有进程;互斥锁是局部的,只串行有并发问题的代码

# 注意与同步锁的区别
from  multiprocessing import  Process,Lock
import time

def task(name,mutex):
    mutex.acquire()
    print('%s 1'%name)
    time.sleep(1)
    print('%s 2' % name)
    time.sleep(1)
    print('%s 3' % name)
    time.sleep(1)
    mutex.release()

if __name__ == '__main__':
    mutex = Lock()
    for i in range(3):
        p = Process(target=task,args=('进程%s'%i,mutex))
        p.start()

 队列:进程间通信(IPC),共享内存

  1、使用内存空间,不宜放大数据

  2、先进先出

from multiprocessing import queues
# queue优点:线程安全
# 先进先出队列
q = queues.Queue(3)
q.put([3,3,3])
q.put('hello')
q.put({'a':1})
print(q.full())
q.put('h')

 

posted @ 2018-03-08 16:03  Claire_xu  阅读(139)  评论(0编辑  收藏  举报