Last active
May 7, 2017 21:25
-
-
Save AndreCAndersen/e029a4c68ee42ab3512cca7e0575a4da to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import inspect | |
import types | |
def execute_source(callback_imports, callback_name, callback_source, args): | |
for callback_import in callback_imports: | |
exec(callback_import, globals()) | |
exec('import time' + "\n" + callback_source) | |
callback = locals()[callback_name] | |
return callback(*args) | |
def submit(executor, callback, *args): | |
callback_source = inspect.getsource(callback) | |
callback_imports = list(imports(callback.__globals__)) | |
callback_name = callback.__name__ | |
future = executor.submit( | |
execute_source, | |
callback_imports, callback_name, callback_source, args | |
) | |
return future | |
def imports(callback_globals): | |
for name, val in list(callback_globals.items()): | |
if isinstance(val, types.ModuleType) and val.__name__ != 'builtins' and val.__name__ != __name__: | |
import_line = 'import ' + val.__name__ | |
if val.__name__ != name: | |
import_line += ' as ' + name | |
yield import_line |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment