python17进程

 

 

 

import os
import time
from multiprocessing.dummy import Process


def so_sth(name):
    print("进程名称{},{}".format(name, os.getpid()))
    time.sleep(5)
    print("进程ok")


#面向对象实现
class MyProcess(Process):

    def __init__(self,name,*args,**kwargs):
        self.myname = name#不能用self.name,因为这个父类里面已经有了
        #这个变量,下面调用父类的会出错
        super().__init__(*args,**kwargs)

    def run(self):
        print("进程名称{},{}".format(self.myname, os.getpid()))
        time.sleep(5)
        print("进程ok")



if __name__ == "__main__":
    #p = Process(target=so_sth,args=("quan",))
    p = MyProcess("myprocessclass")
    p.start()
    p.join()

输出结果:

D:\anaconda\python.exe D:/PycharmProjects/untitled0406/test1.py
进程名称myprocessclass,12436
进程ok

 

 

 

 

 

 

import time
import random
from multiprocessing.dummy import Process
from queue import Queue


class WriteProcess(Process):
    """写的进程"""
    def __init__(self,q,*args,**kwargs):
        self.q = q
        super().__init__(*args,**kwargs)

    def run(self):
        """实现写进程的业务逻辑"""
        ls = [
            "11111111111",
            "22222222222",
            "33333333333",
            "44444444444",
        ]

        for line in ls:
            #写入
            print("x写入内容{}".format(line))
            self.q.put(line)
            time.sleep(random.randint(1,5))

class ReadProcess(Process):
    """读的进程"""
    def __init__(self, q, *args, **kwargs):
        self.q = q
        super().__init__(*args, **kwargs)

    def run(self):
        while True:
            content = self.q.get()
            print("读取达到的内容为{}".format(content))


if __name__ == "__main__":
    #通过Queue共享数据
    q = Queue()
    t_write = WriteProcess(q)
    t_write.start()
    #读取进程
    t_read= ReadProcess(q)
    t_read.start()

    t_write.join()
    #t_read.join()#由于读进程是死循环,无法等待其结束,只能强制终止
    t_read.terminate()

#####################
#Queue通过实例传给两个不同的进程,



结果:
x写入内容11111111111
读取达到的内容为11111111111
x写入内容22222222222
读取达到的内容为22222222222
x写入内容33333333333
读取达到的内容为33333333333
x写入内容44444444444
读取达到的内容为44444444444

 

 

import time
from multiprocessing.context import Process
import random


class WP(Process):
    """写入文件"""

    def __init__(self,file_name,num,*args,**kwargs):
        self.file_name = file_name
        self.num = num
        super().__init__(*args,**kwargs)

    def run(self):
        for i in range(5):
            content = "现在是:{0},{1}.{2}\n".format(
                self.name,
                self.pid,
                self.num
            )
            with open(self.file_name, "a+",encoding="utf-8") as f:
                f.write(content)
                time.sleep(random.randint(1,5))
                print(content)



if __name__ == "__main__":
    file_name = "test.txt"
    for x in range(5):
        p = WP(file_name,x)
        p.start()



结果:
现在是:WP-3,18280.2
现在是:WP-2,2004.1
现在是:WP-1,17244.0
现在是:WP-5,17600.4
现在是:WP-3,18280.2
现在是:WP-4,17712.3
现在是:WP-5,17600.4
现在是:WP-1,17244.0
现在是:WP-2,2004.1
现在是:WP-5,17600.4
现在是:WP-2,2004.1
现在是:WP-3,18280.2
现在是:WP-4,17712.3
现在是:WP-5,17600.4
现在是:WP-5,17600.4
现在是:WP-1,17244.0
现在是:WP-2,2004.1

现在是:WP-1,17244.0
现在是:WP-4,17712.3
现在是:WP-3,18280.2
现在是:WP-1,17244.0
现在是:WP-4,17712.3
现在是:WP-2,2004.1
现在是:WP-4,17712.3

从上面可以看出写的是杂乱无章的

我们可以加个进程锁

import time
from multiprocessing.context import Process
import random
from multiprocessing import Lock


class WP(Process):
    """写入文件"""

    def __init__(self,file_name,num,lock,*args,**kwargs):
        self.file_name = file_name
        self.num = num
        self.lock = lock
        super().__init__(*args,**kwargs)

    def run(self):

        try:
            self.lock.acquire()
            for i in range(5):
                content = "现在是:{0},{1}.{2}\n".format(
                    self.name,
                    self.pid,
                    self.num
                )
                with open(self.file_name, "a+",encoding="utf-8") as f:
                    f.write(content)
                    time.sleep(random.randint(1,5))
                    print(content)
        finally:
            self.lock.release()


if __name__ == "__main__":
    file_name = "test.txt"
    lock = Lock()
    for x in range(5):
        p = WP(file_name,x,lock)
        p.start()




结果:
现在是:WP-1,9852.0
现在是:WP-1,9852.0
现在是:WP-1,9852.0
现在是:WP-1,9852.0
现在是:WP-1,9852.0
现在是:WP-4,18316.3
现在是:WP-4,18316.3
现在是:WP-4,18316.3
现在是:WP-4,18316.3
现在是:WP-4,18316.3
现在是:WP-2,15132.1
现在是:WP-2,15132.1
现在是:WP-2,15132.1
现在是:WP-2,15132.1
现在是:WP-2,15132.1
现在是:WP-5,18260.4
现在是:WP-5,18260.4
现在是:WP-5,18260.4
现在是:WP-5,18260.4
现在是:WP-5,18260.4
现在是:WP-3,17568.2
现在是:WP-3,17568.2
现在是:WP-3,17568.2
现在是:WP-3,17568.2
现在是:WP-3,17568.2
还有Rlock,可以进行多重锁,
对于锁忘记释放,也可以是哟个位置参数进行防止
with self.lock:

 

 

import random
import time
from multiprocessing.pool import Pool

from multiprocessing.process import current_process


def run(file_name,num):
    """
    进程执行的业务逻辑
    往文件中写入数据
    :param file_name:
    :param num:
    :return:
    """
    with open(file_name,"a+",encoding="utf-8") as f:
        #当前进程
        now_process = current_process()
        content = "{0}  --{1}  --{3}".format(
            now_process.name,
            now_process.pid,
            num
        )
        f.write(content)
        f.write("\n")
        #写完随机休息1-5秒
        time.sleep(random.randint(1,5))
        print(content)
    return "ok"


if __name__ == "__main__":
    file_name = "test_pool.txt"
    #进程池
    pool = Pool(2)
    for i in range(20):
        rest = pool.apply(run,args=(file_name,i))
        print("{0}---{1}".format(i,rest))

    pool.close()
    pool.join()

 

posted @ 2020-04-10 21:25  linux——quan  阅读(151)  评论(0编辑  收藏  举报