admin管理员组文章数量:1389777
What I want to do: I have CPU intensive computation that I want to offload to a worker process so as not to block my main server.
Without ProcessPoolExecutor my code looks like this:
my_model = model_init()
def handler(args):
return my_model.process(args)
How do I utilize ProcessPoolExecutor?
consider the function that creates a ProcessPoolExecutor:
concurrent.futures.ProcessPoolExecutor(max_workers=1, initializer=init_function, initargs=(model_name))
say I have:
def init_function():
model = model_init()
The core of the problem is how to "persist" the model
created in the init_function
? Later on I want to submit
tasks to the ProcessPoolExecutor and be able to use the model
created in the init_function
to process the task.
Its difficult to explain - the issue is that ProcessPoolExecutor only knows about two functions the initializer and the function I will provide in submit
. I don't know how to pass state between these functions. How can the function that is argument to submit
access a variable that was initialized when initializer
was called?
The only example I found is here and that does not go over this issue.
What I want to do: I have CPU intensive computation that I want to offload to a worker process so as not to block my main server.
Without ProcessPoolExecutor my code looks like this:
my_model = model_init()
def handler(args):
return my_model.process(args)
How do I utilize ProcessPoolExecutor?
consider the function that creates a ProcessPoolExecutor:
concurrent.futures.ProcessPoolExecutor(max_workers=1, initializer=init_function, initargs=(model_name))
say I have:
def init_function():
model = model_init()
The core of the problem is how to "persist" the model
created in the init_function
? Later on I want to submit
tasks to the ProcessPoolExecutor and be able to use the model
created in the init_function
to process the task.
Its difficult to explain - the issue is that ProcessPoolExecutor only knows about two functions the initializer and the function I will provide in submit
. I don't know how to pass state between these functions. How can the function that is argument to submit
access a variable that was initialized when initializer
was called?
The only example I found is here and that does not go over this issue.
Share Improve this question asked Mar 13 at 23:55 morpheusmorpheus 20.4k29 gold badges110 silver badges188 bronze badges 1- Does this model have a mutable state? Specifically, will the worker modify it? – Lukasz Tracewski Commented Mar 14 at 6:44
1 Answer
Reset to default 1This is generally a place where it makes sense to use a global variable. The global variable will be used only within the child process, so you don't need to worry about different processes using the same value and tripping over each other.
Try:
def init_func(*args):
global my_model
my_model = model_init(*args) # or do whatever is appropriate with the args
def process_func(*args)
my_model.process(*args)
本文标签: Understanding state management in Python ProcessPoolExecutorStack Overflow
版权声明:本文标题:Understanding state management in Python ProcessPoolExecutor - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744679588a2619319.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论