문제

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?

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top