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()
python17进程的更多相关文章
- Python17个常用内置模块总结
Python17个常用内置模块总结 1.getpass 2.os 3.sys 4.subprocess 5.hashlib 6.json 7.pickle 8.shutil 9.time 10.dat ...
- 故障重现, JAVA进程内存不够时突然挂掉模拟
背景,服务器上的一个JAVA服务进程突然挂掉,查看产生了崩溃日志,如下: # Set larger code cache with -XX:ReservedCodeCacheSize= # This ...
- 死磕内存篇 --- JAVA进程和linux内存间的大小关系
运行个JAVA 用sleep去hold住 package org.hjb.test; public class TestOnly { public static void main(String[] ...
- 使用Monit监控本地进程
目前用它监控某些服务,失败自动重启,同时监控特定的日志文件,如果有变化,就发邮件报警 安装不细写了,网上好多 我先用cat /proc/version看了下我的系统是el6的,于是wget http: ...
- [APUE]进程控制(上)
一.进程标识 进程ID 0是调度进程,常常被称为交换进程(swapper).该进程并不执行任何磁盘上的程序--它是内核的一部分,因此也被称为系统进程.进程ID 1是init进程,在自举(bootstr ...
- [APUE]UNIX进程的环境(下)
一.共享库 共享库使得可执行文件中不再需要包含常用的库函数,而只需在所有进程都可存取的存储区中保存这种库例程的一个副本.程序第一次执行的时候或第一次调用某个库函数的时候,用动态链接方法将程序与共享库函 ...
- Tomcat shutdown执行后无法退出进程问题排查及解决
问题定位及排查 上周无意中调试程序在Linux上ps -ef|grep tomcat发现有许多tomcat的进程,当时因为没有影响系统运行就没当回事.而且我内心总觉得这可能是tomcat像nginx一 ...
- 查看w3wp进程占用的内存及.NET内存泄露,死锁分析
一 基础知识 在分析之前,先上一张图: 从上面可以看到,这个w3wp进程占用了376M内存,启动了54个线程. 在使用windbg查看之前,看到的进程含有 *32 字样,意思是在64位机器上已32位方 ...
- 探索ASP.NET MVC5系列之~~~6.Session篇(进程外Session)
其实任何资料里面的任何知识点都无所谓,都是不重要的,重要的是学习方法,自行摸索的过程(不妥之处欢迎指正) 汇总:http://www.cnblogs.com/dunitian/p/4822808.ht ...
随机推荐
- 自动化键盘,python
国际惯例先上源https://github.com/boppreh/keyboard#键盘输入 from pynput.keyboard import Key,Controller,Listener ...
- 攻防世界 web4.cookie
题有几种解法,我有点懒,懒的打开burp,所以可以直接在浏览器拿flag, 首先访问ip/cookie.php,提示:See the http response 接着F12查看响应头 给你cyberp ...
- word-break leetcoder C++
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...
- Win10自动备份oracle数据库
1.环境 操作系统:win10 数据库: 2.创建backup.bat文件 [ @echo offset name=%date:~0,4%%date:~5,2%%date:~8,2%set backu ...
- 印象最深的一个bug——排查修复问题事件BEX引发的谷歌浏览器闪退崩溃异常
前言 最近,我们部门负责项目运维的小王频频接到甲方的反馈,运行的项目使用谷歌浏览器登录后,每次点击处理2秒后,浏览器自动闪退崩溃.小王同学折腾了一个星期,还没找到问题的原因.甲方客户都把问题反馈给项目 ...
- php swoft redis 发布和订阅
//订阅 public function subscribe() { /* @var \Swoft\Redis\Redis $redis */ $redis = App::getBean(\Swoft ...
- 组件通信之全局事件总线 & 消息订阅发布
全局事件总线 介绍 一种组件间通信的方式,适用于任意组件间通信. 在使用全局事件总线之前需要一些知识准备 所有组件实例的原型对象的原型对象就是 Vue 的原型对象,即VueComponent.prot ...
- 物联网3D,物业基础设施3D运维,使用webgl(three.js)与物联网设备结合案例。搭建智慧楼宇,智慧园区,3D园区、3D物业设施,3D楼宇管理系统——第八课
写在前面的废话: 很久没有更新文章了,这段时间一直忙于项目落地,虽然很忙,但是感觉没有总结,没有提炼的日子,总是让人感觉飘飘忽忽的. 所幸放下一些事,抽出一些时间,把近期的项目做一些整理与记录.也算是 ...
- Centos8 部署 ElasticSearch 集群并搭建 ELK,基于Logstash同步MySQL数据到ElasticSearch
Centos8安装Docker 1.更新一下yum [root@VM-24-9-centos ~]# yum -y update 2.安装containerd.io # centos8默认使用podm ...
- C/C++ QT QChart 绘制组件应用
QtCharts 组件是QT中提供图表绘制的模块,该模块可以方便的绘制常规图形,Qtcharts 组件基于GraphicsView模式实现,其核心是QChartView和QChart的二次封装版. 在 ...