简体   繁体   English

快速 API 拨打 python

[英]Fast API calls in python

i am building a package in python. Package will be probably used in Jupyter notebooks.我正在 python 中构建一个 package。Package 可能会用于 Jupyter 笔记本。 I used synchonized code for some functionality, but it is slow so i am planning to use asyncio我为某些功能使用了同步代码,但速度很慢,所以我打算使用 asyncio

The question is how to implement it with async functionality without changing the code in notebook.问题是如何在不更改笔记本代码的情况下使用异步功能实现它。 So user will be able to eg module.orders("Example Inc") Now user has to make initial setup eg loop = asyncio.get_event_loop() loop.run_until_complete(module.orders("Example Inc"))所以用户将能够例如 module.orders("Example Inc") 现在用户必须进行初始设置,例如loop = asyncio.get_event_loop() loop.run_until_complete(module.orders("Example Inc"))

I want to avoid this, so it will be easier to use Thanks Or is there some better way fo make fast GET api calls我想避免这种情况,所以使用起来会更容易 谢谢 或者有没有更好的方法来快速调用 GET api

want eg module.orders("Example Inc") avoid this loop = asyncio.get_event_loop() loop.run_until_complete(module.orders("Example Inc"))想要例如 module.orders("Example Inc") 避免这个loop = asyncio.get_event_loop() loop.run_until_complete(module.orders("Example Inc"))

The solusion was relatively easy.解决方案相对容易。 To obtain paralelized get requests with use of asyncio and aiohttp python packages and still have option to use it as 'synchronized code' and avoid async functionality to go up, to other functions.要使用 asyncio 和 aiohttp python 包获得并行化的 get 请求,并且仍然可以选择将其用作“同步代码”并避免异步功能到 go 向上,到其他功能。

async def getter(params):
    # get data from server 
    c = aiohttp.TCPConnector(limit=100)
    async with aiohttp.ClientSession(connector=c) as session:
        # send requests

def orders(name):
    # no need of async
    return asyncio.run(getter(params))


dataset = orders("company")

Code above is preudocode.上面的代码是伪代码。

for usage with jupyter notebbok it is needed to have与 jupyter notebbok 一起使用需要有

import nest_asyncio
nest_asyncio.apply()

GET requests are faster, but probably there is another bottleneck on the server side GET 请求更快,但服务器端可能存在另一个瓶颈

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM