The foo
variable is scoped at the module level, in the same module that bar
is defined in. Your code should run fine without importing foo
. See Short Description of the Scoping Rules? for more information.
Python, can you pass a module level function to ThreadPoolExecutor from within a class
-
24-09-2022 - |
Question
I'm trying to perform multithreading in python and am still trying to get my head around the picklability requirement and how much I can keep my code OOP.
Can you run ThreadPoolExecutor from within a class passing it a function defined at the module level? My setup is as follows:
moduleA.py:
def foo():
run some stuff
class bar(object):
def runThreads(self):
executor = futures.ThreadPoolExecutor(5)
executor.submit(foo) # do this 5 times in some sort of loop
I'd like to be able to call this from a separate module
moduleB.py
from moduleA import bar
A = bar()
A.runThreads()
Will this work? Do I need to import foo as well?
Solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow