本文实例讲述了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() # 多