External API calls during module __init__. Terrible idea or fine?

Hello everyone,

I have the following module:

module SideEffects

function __init__()
 # load_or_fetch! does an API call to a service to retrieve a list
# OR deserialises an object, if the call has already been done
 global MY_DICT = load_or_fetch!(:list)

end

I am not sure if this is a terrible idea, but it worked fine with Julia 1.9 and 1.10 (a bit less sure about that). When updating to 1.11, it gets stuck precompiling because of a I/O task in the background:

Waiting for background task / IO / timer

So I have two questions:

  • Is there a better way to achieve this?
  • Should I just try to have a completely different approach?

It is very convenient to have this list as sort of a “module constant” and be able to do SideEffects.MY_DICT from multiple places in my code.

Thanks in advance!

Is it possible in your context to wrap this external data in an artifact? This would be more stable & probably more reproducible than downloading some data during init.

Possibly! Somehow I completely forgot about this. I will check it out, thanks for the help.

Perhaps it’s a problem for the update of the artifact itself, perhaps I can work around it.

EDIT: The problem is that in this case maybe it should be immutable and that might be somewhat inconvenient, but I think I should be able to work around it.