Python进程,多进程,获取进程id,给子进程传递参数操作示例
作者:houyanhua1 时间:2021-01-13 09:39:47
本文实例讲述了Python进程,多进程,获取进程id,给子进程传递参数操作。分享给大家供大家参考,具体如下:
线程与线程之间共享全局变量,进程之间不能共享全局变量。
进程与进程相互独立 (可以通过socket套接字实现进程间通信,可以通过硬盘(文件)实现进程通信,也可以通过队列(Queue)实现进程通信)
子进程会拷贝复制主进程中的所有资源(变量、函数定义等),所以子进程比子线程耗费资源。
demo.py(多进程):
import threading # 线程
import time
import multiprocessing # 进程
def test1():
while True:
print("1--------")
time.sleep(1)
def test2():
while True:
print("2--------")
time.sleep(1)
def main():
# t1 = threading.Thread(target=test1) # 线程
# t2 = threading.Thread(target=test2)
# t1.start() # 多线程的方式实现多任务
# t2.start()
p1 = multiprocessing.Process(target=test1) # 进程 (进程比线程占用资源多)
p2 = multiprocessing.Process(target=test2)
p1.start() # 多进程的方式实现多任务 (进程比线程占用资源多)
p2.start()
if __name__ == "__main__":
main()
demo.py(获取进程、父进程id):
import multiprocessing
import os
import time
def test():
while True:
print("----in 子进程 pid=%d ,父进程的pid=%d---" % (os.getpid(), os.getppid()))
time.sleep(1)
def main():
# os.getpid() 获取当前进程的进程id
# os.getppid() 获取当前进程的父进程id
print("----in 主进程 pid=%d---父进程pid=%d----" % (os.getpid(), os.getppid()))
p = multiprocessing.Process(target=test)
p.start() # 开启子进程
if __name__ == "__main__":
main()
demo.py(给子进程传递参数):
import multiprocessing
def test(a, b, c, *args, **kwargs):
print(a) # 11
print(b) # 22
print(c) # 33
print(args) # (44, 55, 66, 77, 88)
print(kwargs) # {'age': 20, 'name': '张三'}
def main():
p = multiprocessing.Process(target=test, args=(11, 22, 33, 44, 55, 66, 77, 88), kwargs={"name": "张三","age": 20})
p.start()
if __name__ == "__main__":
main()
希望本文所述对大家Python程序设计有所帮助。
来源:https://blog.csdn.net/houyanhua1/article/details/84330952
标签:Python,进程,多进程
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python基础教程之Hello World!
2021-03-05 22:45:06
深入浅析Python中的yield关键字
2022-04-18 05:02:32
Python input函数使用实例解析
2022-01-05 21:00:34
Python2与Python3的区别点整理
2022-02-23 07:44:46
python获取服务器响应cookie的实例
2023-06-14 15:02:50
python中wheel的用法整理
2022-07-03 18:15:47
Python利用Turtle绘制哆啦A梦和小猪佩奇
2022-05-08 08:38:24
![](https://img.aspxhome.com/file/2023/2/77822_0s.png)
oracle 彻底删除方法
2009-07-02 12:22:00
Python中方法的缺省参数问题解读
2022-10-07 17:00:45
![](https://img.aspxhome.com/file/2023/9/92059_0s.png)
使用实例管理器轻松管理多个MySQL实例
2009-03-25 17:07:00
Python地图四色原理的遗传算法着色实现
2022-10-07 20:57:45
![](https://img.aspxhome.com/file/2023/5/75955_0s.jpg)
用python生成1000个txt文件的方法
2023-03-24 07:51:11
![](https://img.aspxhome.com/file/2023/0/99540_0s.jpg)
PHP children()函数讲解
2023-06-13 04:38:38
![](https://img.aspxhome.com/file/2023/9/55409_0s.png)
Python Pandas工具绘制数据图使用教程
2023-02-08 01:16:40
![](https://img.aspxhome.com/file/2023/3/72003_0s.png)
python面向对象 反射原理解析
2021-05-14 08:56:32
Python+pyftpdlib实现局域网文件互传
2022-01-10 04:09:30
![](https://img.aspxhome.com/file/2023/2/112782_0s.png)
DW表格应用之细线框的制作
2008-02-03 19:00:00
![](https://img.aspxhome.com/file/UploadPic/20082/3/20082319946365s.gif)
python爬虫 正则表达式使用技巧及爬取个人博客的实例讲解
2023-11-06 02:16:54
![](https://img.aspxhome.com/file/2023/0/67800_0s.jpg)
python网络爬虫实现发送短信验证码的方法
2021-03-23 23:18:54
![](https://img.aspxhome.com/file/2023/6/97596_0s.jpg)
Python3 JSON编码解码方法详解
2023-10-12 15:46:43