I was looking for an asynchronous library for imap and I found this library called imap_tools and find it very easy to use and really want to stick with it but its not asynchronous. The reason I want it to be asynchronous is because I want to use it inside discord.py and non-async network operations would block the event loop and especially because sometimes the response takes longer or gets stuck or anything like that and affects the performance of my bot.
Is there anything I can do to use this library in asynchronous manner like I just need to connect with imap server and fetch an email and that's it. Is it possible that I can just wrap that functionality in an async function?
from imap_tools import MailBox, AND
import asyncio
async def fetch_emails():
with MailBox('imap.gmail.com').login('[email protected]', 'pwd') as mailbox:
for msg in mailbox.fetch():
print(msg.subject)
asyncio.run(fetch_emails())
This is from another question, but maybe it could help you. Because the main async loop runs in a single thread, non asynchronous tasks will block the entire loop and therefore must be run in a separate thread.
See here for more information (this code below is from this answer here): https://stackoverflow.com/a/28492261/238849