mailerlite-api-python icon indicating copy to clipboard operation
mailerlite-api-python copied to clipboard

use aiohttp and asyncio

Open skoudoro opened this issue 3 years ago • 4 comments

The current performance is ok but Faster is better.

For learning purposes and to improve the performance of huge mailing lists, it would be good to use aiohttp and asyncio.

EDIT: e.g: https://towardsdatascience.com/fast-and-async-in-python-accelerate-your-requests-using-asyncio-62dafca83c33

skoudoro avatar May 26 '21 19:05 skoudoro

Actually the current performance is pretty bad, for me its coming in about 5 seconds slower than the raw API.

This package does seem useful, it's just right now, I cannot use it at that speed. I'm using Python 3.9.

I've compared it actually in Jupyter and on a dedicated Docker system and both come back within a second using the native API in the docs.

Are you using V2 of the API ???

akblissweb avatar Feb 28 '22 10:02 akblissweb

I just checked you are indeed using V2.

akblissweb avatar Feb 28 '22 10:02 akblissweb

Its this line that is causing the performance issue...

api = MailerLiteApi('xxxxx')

Because I'm using it in a web page I have to run this for every request.

In Jupyter its not so bad once I've created the api object, the actual API calls come back in a reasonable time.

akblissweb avatar Feb 28 '22 10:02 akblissweb

Hi @akblissweb,

Because I'm using it in a web page I have to run this for every request.

Can you provide an example? if you have a server running, I do not understand why you need to call api = MailerLiteApi('xxxxx') every time. I might be missing something. Can you describe more in detail your pipeline?

To be faster:

  • you do not need to instantiate all the objects. This is what api = MailerLiteApi('xxxxx') is doing because it is convenient. You can just pick what you really need. For example, if you need only the campaign on a specific webpage, just do from mailerlite.campaign import Campaigns; c = Campaigns('xxxxx').
  • You can return only the JSON object in each function instead of the convenient custom nametuple/dataclass. When you make a call, add the parameter as_json=True. It should be faster and give you the raw JSON to do whatever you want.

Also, any other suggestions/ideas/contributions are welcomed.

Thank you for the feedback

skoudoro avatar Feb 28 '22 14:02 skoudoro