PythonDay05---from DFZY

2019.8.21

进程

Rlock

import multiprocessing

def A(lock,i):
    print(i)
if __name__ == "__main__":
    lock = multiprocessing.Rlock()
    process_list = []
    for i in range(10):
        p = multiprocessing.Process(target=A,args=(lock,i))
        p.start()
        process_list.append(p)

    for p in process_list:
        p.join()

队列

import multiprocessing
import time


def PUT(q):
    for i in range(10):
        time.sleep(1)
        q.put(100)

def GET(q):
    while 1:
        try:
            print('GET',q.get(timeout = 5))
        except:
            print('队列中已经无法获取元素在5秒之内')
            break

if __name__ == "__main__":
    q = multiprocessing.Queue()
    p1 = multiprocessing.Process(target=PUT,args=(q,))
    p2 = multiprocessing.Process(target=GET,args=(q,))
    p1.start()
    p2.start()
    p1.join()
    p2.join()

聊天机器人

#聊天机器人
import multiprocessing

def M(conn):
    while 1:
        recv_ = conn.recv()
        print('机器人收到:%s'%recv_)
        if recv_ == "你好":
            conn.send('我不好')
        elif recv_ == "今天天气好":
            conn.send('自己看')
        elif recv_ == "该做什么":
            conn.send('你想做什么')
        elif recv_ == "我有钱吗":
            conn.send('没逼数')
        else:
            conn.send('别问我')

def P(conn):
    y = 'y'
    while y != 'n':
        input_ = input('人说:')
        conn.send(input_)
        print('say %s'%conn.recv())


if __name__ == "__main__":
    conn_M,conn_p = multiprocessing.Pipe()
    p_M = multiprocessing.Process(target=M,args=(conn_M,))
    p_M.start()
    P(conn_p)

队列
PythonDay05---from DFZY

PythonDay05---from DFZY

队列Queue

上一篇:基于select的sokct ftp 服务端


下一篇:Python socket