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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top