Hi all, is there a way to force a reload of the co...
# ask-metaflow
h
Hi all, is there a way to force a reload of the configurations in memory (using a notebook to fetch metaflow artifacts from multiple different metaflow server environments and just changing the METAFLOW_PROFILE doesn't work.
BUMP: What I am facing seems to be related to the fact that once the import happens, it looks at the json configuration file on disk, but even if I point the METAFLOW_PROFILE environment variable to a different file, or even change the file content, the cell will run against the same exact config unless I restart the kernel for the notebook. is there a way to force a full reload of the configuration file in a python env?
v
right, today it's loaded once when the module is imported
let me see if there's a workaround..
👍 1
it's tricky since we don't support switching profiles on the fly within a process today, but here's a workaround:
Copy code
import os
from multiprocessing import Pool

def latest_run(flow_name):
    from metaflow import Flow
    return Flow(flow_name).latest_run.pathspec

def call(op, args, profile=None):
    os.environ['METAFLOW_PROFILE'] = profile
    with Pool(processes=1) as pool:
        return pool.apply(op, args)

if __name__ == '__main__':
    print(call(latest_run, ('HelloFlow',), profile='local'))
    print(call(latest_run, ('HelloFlow',), profile='mozart'))
i.e. if you can implement the operations you need as a function like
latest_run
here, then you can call them via
call
(or you can craft a fancier abstraction)
h
thanks, will have a look at how this works!
👍 1
Update, we tried multiple ways to implement that subprocess idea, but sadly we haven't been able to make it work. Is there a way this could be supported by design? I am unclear what it would require from the code, but it feels like a form of init function or parameter could work?
v
opened an issue here to track it - certainly it'd be a useful feature but it's not a totally trivial change
🙌 1
did you get the above example working at all or is there something specific in your situation why the workaround wouldn't work?
h
no matter how much we tried, we could never get the subprocess to work, but I suspect it maybe because of something lost in translation between python/ipython/metaflow (due to being in a notebook)
v
did you get the above example working, since it should work in a notebook fine? It does need to serialize return values from the subprocess to the main process, so if you try to return a complex/unpicklable object, that can get tricky but there may be ways to work around such serialization issues
h
We've had multiple different errors, but as is (at its minimum, the way you suggested), the first error I get is it's unable to get $USERNAME once I add
os.environ['METAFLOW_USERNAME'] = hex
to the call function, then I get this error:
Copy code
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
hex_cell_3f45a3d9-0ae9-4d5a-9e68-dd86735dfe50.py in <module>
     13 
     14 if __name__ == '__main__':
---> 15     print(call(latest_run, ('HelloFlow',), profile='haus_prod'))

hex_cell_3f45a3d9-0ae9-4d5a-9e68-dd86735dfe50.py in call(op, args, profile)
      7 
      8 def call(op, args, profile=None):
----> 9     os.environ['METAFLOW_USERNAME'] = hex
     10     os.environ['METAFLOW_PROFILE'] = profile
     11     with Pool(processes=1) as pool:

/usr/local/lib/python3.10/os.py in __setitem__(self, key, value)
    683     def __setitem__(self, key, value):
    684         key = self.encodekey(key)
--> 685         value = self.encodevalue(value)
    686         putenv(key, value)
    687         self._data[key] = value

/usr/local/lib/python3.10/os.py in encode(value)
    755         def encode(value):
    756             if not isinstance(value, str):
--> 757                 raise TypeError("str expected, not %s" % type(value).__name__)
    758             return value.encode(encoding, 'surrogateescape')
    759         def decode(value):

TypeError: str expected, not builtin_function_or_method