My python code is working fine outside of FME but it fails inside of a PythonCaller. The documentation doesn't give any information about the scope or execution context in which the code inside a PythonCaller is run.
What are the restrictions when running python code inside a PythonCaller? Why does it not run the same code that you can run outside of FME? (while using the same installation of python in FME and on the command line)
I'm trying to use concurrent.futures.ProcessPoolExecutor for parallel processing in Python inside a PythonCaller but I'm getting this error:
from concurrent.futures import ProcessPoolExecutor
def test(x):
return x
with ProcessPoolExecutor(16) as executor:
result = executor.map(test, range(0,10))
next(result)
Python Exception <PicklingError>: Can't pickle <function test at 0x000000000F6A8F70>: attribute lookup test on __main__ failed
concurrent.futures.process._RemoteTraceback:
"""
Traceback (most recent call last):
File "multiprocessing\queues.py", line 244, in _feed
File "multiprocessing\reduction.py", line 51, in dumps
_pickle.PicklingError: Can't pickle <function test at 0x000000000F6A8F70>: attribute lookup test on __main__ failed
"""
ProcessPoolExecutor relies on the pickle module so I ran this code and got the same error:
import pickle
def test():
pass
pickle.dumps(test)
Python Exception <PicklingError>: Can't pickle <function test at 0x000000000F698F70>: attribute lookup test on __main__ failed
Traceback (most recent call last):
File "<string>", line 7, in <module>
_pickle.PicklingError: Can't pickle <function test at 0x000000000F698F70>: attribute lookup test on __main__ failed
This problem only occurs in FME, not if I run the same Python script outside of FME. I did a lot of research on the internet and it essentially always leads me to this section in the docs: What can be pickled and unpickled? It says functions accessible from the top level of a module can be pickled. And test is a function at the top level of the module. You can check by printing globals().
Why does the attribute lookup for test on __main__ fail when running my code in FME?