首页 > 系统相关 >多进程 multiprocessing in Python

多进程 multiprocessing in Python

时间:2023-01-19 17:13:04浏览次数:40  
标签:do start Python print something time 进程 multiprocessing

Simple example

import time
import multiprocessing

start = time.perf_counter()

def do_something():
	print('sleeping 1 second...')
	time.sleep(1)
	print('Done sleeping...')


p1 = multiprocessing.Process(target = do_something)
p2 = multiprocessing.Process(target = do_something)

p1.start()
p2.start()

p1.join()
p2.join()

finish = time.perf_counter()

print(f'Finish in {round(finish-start,2)} seconds(s)')

for循环

import time
import multiprocessing

start = time.perf_counter()

def do_something():
	print('sleeping 1 second...')
	time.sleep(1)
	print('Done sleeping...')

processes = []
for  _ in range(10):
	p = multiprocessing.Process(target = do_something)
	p.start()
	processes.append(p)

for process in processes:
	process.join()

finish = time.perf_counter()

print(f'Finish in {round(finish-start,2)} seconds(s)')

加个参数

import time
import multiprocessing

start = time.perf_counter()

def do_something(seconds):
	print(f'sleeping {seconds} second...')
	time.sleep(seconds)
	print('Done sleeping...')

processes = []
for  _ in range(10):
	p = multiprocessing.Process(target = do_something, args = [2])
	p.start()
	processes.append(p)

for process in processes:
	process.join()

finish = time.perf_counter()

print(f'Finish in {round(finish-start,2)} seconds(s)')

标签:do,start,Python,print,something,time,进程,multiprocessing
From: https://www.cnblogs.com/conpi/p/17061793.html

相关文章

  • python操作mysql基础
    importpymysqlconfig={'host':'127.0.0.1','port':3306,'user':'root','password':'root','database':'sys','cursorclass':......
  • Python3.9安装
    一、安装python3.9链接:https://pan.baidu.com/s/1mDkgKt2KSoMrKVxesb76Pg?pwd=ma4n提取码:ma4n--来自百度网盘超级会员V4的分享下载python3.9,然后安装到D盘都是......
  • 多线程threading in Python
    SimpleExample1importtimeimportthreadingstart=time.perf_counter()defdo_something(): print('sleeping1second...') time.sleep(1) print('Doneslee......
  • Python - requests 使用记录
    requests使用简单方法记录importrequestsfromfake_useragentimportUserAgentua=UserAgent()headers={'User-Agent':ua.random#伪装}#......
  • 51.Oracle有哪些后台进程
    Oracle的进程主要有三种:后台进程、服务器进程、用户进程关系如图所示:1、USERPROCESS(用户进程)用户进程指的是Oracle客户端进程,是连接到 OracleDB的应用程序或工具。......
  • 我的Python程序太慢了。如何加快速度?
    如果你的Python程序太慢,你可以按照下面给出的提示和技巧-抽象化避免过度抽象,尤其是在微小函数或方法的形式下。抽象往往会产生间接性,并迫使解释器工作更多。如果间接寻......
  • (转载)Python中关键词yield怎么用?
    原文:https://stackoverflow.com/questions/231767/what-does-the-yield-keyword-do译文:https://zhuanlan.zhihu.com/p/23276711?refer=passer问题描述:Python中关......
  • Python如何运行程序
    Python如何运行程序Python解释器简介解释器是一种让其他程序运行起来的程序。Python解释器将读取程序,并按照其中的命令执行,得出结果。解释器是代码与机器的计算机硬件......
  • win10下python3.9的代理报错问题解决(附web3的polygon爬虫源码)
    背景因为工作中经常需要代理访问,而开了代理,request就会报错SSLError,如下:requests.exceptions.SSLError:HTTPSConnectionPool(host='test-admin.xxx.cn',port=443):Ma......
  • python3安装
    前言由于CentOS7本身就需要安装Python2.7.5,而且这个Python2不能被删除,因为有很多系统命令,比如yum都要用到。whichpythonPython3的方法首先安装依赖包yum-ygroupi......