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 ...
随机推荐
- kiyv Button参数属性
from kivy.uix.button import Button from kivy.uix.floatlayout import FloatLayout from kivy.app import ...
- [no_code][Alpha]项目展示博客
$( "#cnblogs_post_body" ).catalog() 团队项目链接 github 后端 github OCR文档-含部分所需测试代码目前private API调用 ...
- [技术博客]大闸蟹的技术博客,通过gitlab api进行用户批量创建
技术博客--通过gitlab api批量注册用户 gitlab登录界面本身提供了register功能,但需要手工一个个添加,对于一次性会添加整个班级的学生的软工平台来说并不科学合理.使用gitlab ...
- [火星补锅] 水题大战Vol.2 T2 && luogu P3623 [APIO2008]免费道路 题解
前言: 如果我自己写的话,或许能想出来正解,但是多半会因为整不出正确性而弃掉. 解析: 这题算是对Kruskal的熟练运用吧. 要求一颗生成树.也就是说,最后的边数是确定的. 首先我们容易想到一个策略 ...
- Spring MVC:HandlerMapping
HandlerMapping 的类图 Spring中存在两种类型的handlers.第一种是 handler mappings(处理程序映射).它们的角色定位与前面所描述的功能完全相同.它们尝试将当前 ...
- DDD领域驱动设计架构模式:防腐层(Anti-corruption layer)
在微服务(Microservices)架构实践中,架构设计借用了DDD中的一些概念和技术,比如一个微服务对应DDD中的一个限界上下文(Bounded Context):在微服务设计中应该首先识别出DD ...
- threading python2 和python3
from __future__ import division from __future__ import print_function import threading balance = 0 d ...
- 20191310李烨龙作业:MySort
作业:MySort 任务详情 1. 用man sort 查看sort的帮助文档 2. sort常用选项有哪些,都有什么功能?提交相关使用的截图 3. 如果让你编写sort,你怎么实现?写出伪代码和相关 ...
- 如何抓取直播源及视频URL地址-疯狂URL(教程)
直播源介绍 首先,我们来快速了解一下什么是直播源,所谓的直播源,其实就说推流地址,推流地址可能你也不知道是什么,那么我再简单说一下,推流地址就是,当某个直播开播的时候,需要将自己的直播状态实时的展示给 ...
- Ubuntu安装数据库
1.通过命令行安装:sudo apt-get install mysql-client mysql-server 2.安装过程中输入数据库密码("123456",root) 3.使 ...