python 多进程 多线程 协程

多进程-进程池

1 from concurrent.futures import ProcessPoolExecutor
2 
3 with ProcessPoolExecutor(max_workers=10) as executor:
4         results = executor.map(func, df_list)
5 
6         for result in results:
7             df = pd.concat([df, result], ignore_index=False)

 

协程

import asyncio

#异步获取经纬度程序
async def get_coordinate_ascio(sem, result_dict, index, address):
    async with sem:
        try:
            async with aiohttp.ClientSession() as session:
                api_addr = ''
                async with session.get(api_addr) as resp:
                    response = await resp.text()
            response = json.loads(response)
        except Exception:
            traceback.print_exc()
            print(index)
        await asyncio.sleep(1)

#定义协程
print('定义协程')
#loop = asyncio.get_event_loop()
loop =  asyncio.new_event_loop()
asyncio.set_event_loop(loop)
#设置一秒最大并发数
max_second_qps = 150
sem = asyncio.Semaphore(max_second_qps)
tasks = [get_coordinate_ascio(sem, result_dict,index,row.现场地址) for index,row in df.iterrows()]
wait_coro = asyncio.wait(tasks)

loop.run_until_complete(wait_coro)
loop.close()

 

posted on 2022-11-14 19:39  么么打123  阅读(85)  评论(0编辑  收藏  举报