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()

python17进程的更多相关文章

  1. Python17个常用内置模块总结

    Python17个常用内置模块总结 1.getpass 2.os 3.sys 4.subprocess 5.hashlib 6.json 7.pickle 8.shutil 9.time 10.dat ...

  2. 故障重现, JAVA进程内存不够时突然挂掉模拟

    背景,服务器上的一个JAVA服务进程突然挂掉,查看产生了崩溃日志,如下: # Set larger code cache with -XX:ReservedCodeCacheSize= # This ...

  3. 死磕内存篇 --- JAVA进程和linux内存间的大小关系

    运行个JAVA 用sleep去hold住 package org.hjb.test; public class TestOnly { public static void main(String[] ...

  4. 使用Monit监控本地进程

    目前用它监控某些服务,失败自动重启,同时监控特定的日志文件,如果有变化,就发邮件报警 安装不细写了,网上好多 我先用cat /proc/version看了下我的系统是el6的,于是wget http: ...

  5. [APUE]进程控制(上)

    一.进程标识 进程ID 0是调度进程,常常被称为交换进程(swapper).该进程并不执行任何磁盘上的程序--它是内核的一部分,因此也被称为系统进程.进程ID 1是init进程,在自举(bootstr ...

  6. [APUE]UNIX进程的环境(下)

    一.共享库 共享库使得可执行文件中不再需要包含常用的库函数,而只需在所有进程都可存取的存储区中保存这种库例程的一个副本.程序第一次执行的时候或第一次调用某个库函数的时候,用动态链接方法将程序与共享库函 ...

  7. Tomcat shutdown执行后无法退出进程问题排查及解决

    问题定位及排查 上周无意中调试程序在Linux上ps -ef|grep tomcat发现有许多tomcat的进程,当时因为没有影响系统运行就没当回事.而且我内心总觉得这可能是tomcat像nginx一 ...

  8. 查看w3wp进程占用的内存及.NET内存泄露,死锁分析

    一 基础知识 在分析之前,先上一张图: 从上面可以看到,这个w3wp进程占用了376M内存,启动了54个线程. 在使用windbg查看之前,看到的进程含有 *32 字样,意思是在64位机器上已32位方 ...

  9. 探索ASP.NET MVC5系列之~~~6.Session篇(进程外Session)

    其实任何资料里面的任何知识点都无所谓,都是不重要的,重要的是学习方法,自行摸索的过程(不妥之处欢迎指正) 汇总:http://www.cnblogs.com/dunitian/p/4822808.ht ...

随机推荐

  1. 通过简单例子 | 快速理清 UML 中类与类的六大关系

    关于封面:我想我们都会离开 类与类之间的六大关系 泛化 ( Generalization ) ---> 表继承关系 实现 ( Realization ) 关联 ( Association ) 聚 ...

  2. Scrum Meeting 1补充会议

    日期:2021年04月24日 会议主要内容概述: 本次会议于11:30举行,对项目架构做出了重要调整,并根据该调整修改了第1次例会报告中后两日计划完成的工作部分. 一.架构调整 会上讨论了用户模块相关 ...

  3. Beta阶段第一次会议

    Beta阶段第一次例会 时间:2020.5.16 完成工作 姓名 完成任务 难度 完成度 lm 1.修订网页端信息编辑bug2.修订网页端登录bug(提前完成,相关issue已关闭) 中 100% x ...

  4. mysqld_exporter监控mysql信息

    mysqld_exporter监控mysql信息 一.背景 二.prometheus接入mysqld_exporter 1.安装mysqld_exporter 2.创建mysqld_exporter用 ...

  5. 2021.10.10考试总结[NOIP模拟73]

    T1 小L的疑惑 对于\(P_i\),如果所有比\(P_i\)小的数加起来也达不到\(P_i-1\),那么值域肯定不连续.否则设原来值域最大值为\(mx\),则\(P_i\)会让值域最大值增致\(mx ...

  6. 《基于SIRS模型的行人过街违章传播研究》

    My Focus: 行人违章过街 这一行为的传播与控制 Behavior definition in this paper: 人在生活中表现出来的生活态度及具体的生活方式 Title: Researc ...

  7. 21.10.18 test

    可可大神出题,四款有趣的游戏推荐,第四个好玩/se T1 loopers \(\color{green}{100}\) 考虑钦定 \(a_1,a_i\) 的位置,固定左边一坨,那么剩下的一坨的 \(\ ...

  8. linux中dd命令

    转载:https://www.runoob.com/linux/linux-comm-dd.html Linux dd 命令用于读取.转换并输出数据. dd 可从标准输入或文件中读取数据,根据指定的格 ...

  9. 几十行js实现很炫的canvas交互特效

    几十行js实现很炫的canvas交互特效 废话不多说,先上效果图! 本篇文章的示例代码都是抄的一个叫Franks的老外在yutube上的一个教学视频,他还出了很多关于canvas的视频,十分值得学习, ...

  10. laravel groupby 报错

    报错信息 laravel which is not functionally dependent on columns in GROUP BY clause; this is incompatible ...