1

Currently I fetch emails using an iterator this way:

for item in account.inbox.filter(datetime_received__gt=__LAST_ANALYSYS__+timedelta(0,1)):

I also test if type(item) == Message: as another filter layer.

Issue: it takes 5-10 mins to fetch 400+ messages; I have a macbook pro 3.5 GHz Dual-Core Intel Core i7 and 16 GB 2133 MHz LPDDR3. I have reasonably fast broadband connect.

The app will be migrated to Azure soon. Will the retrieval speed improve?

Or, is there another trick i can employ? I saw a post about using the only(attribs) nethod, but not sure if this helps materially, or how it can be combined with the account.inbox.filter method? Thank you.

2 Answers2

3

Just try out the .only() method and see if it helps your use-case. Usually it does, because without it, you'll be fetching everything; attachments, MIME content, and other heavy fields. If you have a folder where the ratio of Message items to other item types is low, you will also benefit by doing a filter on item_class so you're filtering server-side, not client-side.

.only() supports chaining, just like Django QuerySets: some_folder.filter(...).only(...). See https://ecederstrand.github.io/exchangelib/#searching

Erik Cederstrand
  • 9,643
  • 8
  • 39
  • 63
  • Thanks Erik! I've had a look. I tried the following, but it did not work. Any feedback welcome, TIA. ```for item in account.inbox.filter(datetime_received__gt=__LAST_ANALYSYS__).only('subject','text_body','datetime_received','sender.email_address','cc_recipients'):``` – Patrick Stacey Apr 01 '21 at 10:35
  • How didn't it work? Do you get an error message? You can't use subfields, e.g. `sender.email_address`, with `only()`. You have to specify the full `sender` field. – Erik Cederstrand Apr 01 '21 at 14:59
  • Apols, the trace keeps repeating the following when i try the aforementioned loop: ```DEBUG:exchangelib.util:No retry: wrong status code 200 DEBUG:exchangelib.util:Session 25945 thread 4598377920: Useful response from https://outlook.office365.com/EWS/Exchange.asmx/EWS/Exchange.asmx DEBUG:exchangelib.protocol:Server outlook.office365.com: Releasing session 25945 DEBUG:exchangelib.version:API version "Exchange2016" worked but server reports version "V2018_01_08". Using "Exchange2016" DEBUG:exchangelib.services.common:FindItem: Got page with next offset None (last_page True)``` – Patrick Stacey Apr 01 '21 at 15:56
  • OK sure, will try without the subfield. Many thanks Erik – Patrick Stacey Apr 01 '21 at 15:57
  • 1
    Dear Erik,the script now works perfectly and efficiently! Many thanks for your help. Happy Easter. – Patrick Stacey Apr 01 '21 at 16:01
1

You can use something like multithreading to speed up the process. Save the fetching part in a function and then run this:

threading.Thread(target=(function_name))
Quessts
  • 440
  • 2
  • 19
  • This might not actually help, because the slowness could be caused by the Exchange server being slow. Adding more threads would not help in this case. – Erik Cederstrand Mar 31 '21 at 23:20