Shared variable in concurrent.futures.ProcessPoolExecutor() python

2024/5/20 14:29:36

I want to use parallel to update global variable using module concurrent.futures in python

It turned out that using ThreadPoolExecutor can update my global variable but the CPU did not use all their potential (always at 5-10%), which is so slow

and ProcessPoolExecutor can use all the cores (at 100%) but my global variable can not be updated because they do not share the same global variable

How can I share my global variable using ProcessPoolExecutor in concurrent.futures model. Thank you a lot for your help

Answer

Process doesn't seem like thread that using same memory space. So you need some special way to update variables. ProcessPoolExecutor uses the multiprocessing module, the are two ways for sharing data, Shared memory and Server process. First way using shared memory map, Server process using Manager object that like a proxy to holds sharing data. Server process are more flexible, Shared memory more efficient.

Using Server process sharing data like ThreadPoolExecutor, just pass arguments to you function.

def running_proxy(mval):# consider lock if you needreturn mval.valuedef start_executor():with multiprocessing.Manager() as manager:executor = ProcessPoolExecutor(max_workers=5)mval = manager.Value('b', 1)futures = [executor.submit(running_proxy, mval) for _ in range(5)]results = [x.result() for x in futures]executor.shutdown()

But Shared memory way has some difference, you need setting shared variable to global.

def running_shared():# consider lock if you needreturn sval.valuedef set_global(args):global svalsval = argsdef start_executor():sval = multiprocessing.Value('b', 1)# for 3.7+executor = ProcessPoolExecutor(max_workers=5, initializer=set_global, initargs=(sval,))# for ~3.6# set_global(sval)# executor = ProcessPoolExecutor(max_workers=5)futures = [executor.submit(running_shared) for _ in range(5)]results = [x.result() for x in futures]executor.shutdown()
https://en.xdnf.cn/q/72910.html

Related Q&A

MongoEngine - Another user is already authenticated to this database. You must logout first

Can anyone please explain why I am getting error Another user is already authenticated to this database. You must logout first when connecting to MongoDB using Flask MongoEngine?from mongoengine.conne…

How to bucketize a group of columns in pyspark?

I am trying to bucketize columns that contain the word "road" in a 5k dataset. And create a new dataframe. I am not sure how to do that, here is what I have tried far : from pyspark.ml.featur…

Dictionary of tags in declarative SQLAlchemy?

I am working on a quite large code base that has been implemented using sqlalchemy.ext.declarative, and I need to add a dict-like property to one of the classes. What I need is the same as in this ques…

How to connect to a GObject signal in python, without it keeping a reference to the connecter?

The problem is basically this, in pythons gobject and gtk bindings. Assume we have a class that binds to a signal when constructed:class ClipboardMonitor (object):def __init__(self):clip = gtk.clipboar…

openpyxl please do not assume text as a number when importing

There are numerous questions about how to stop Excel from interpreting text as a number, or how to output number formats with openpyxl, but I havent seen any solutions to this problem:I have an Excel s…

NLTK CoreNLPDependencyParser: Failed to establish connection

Im trying to use the Stanford Parser through NLTK, following the example here.I follow the first two lines of the example (with the necessary import)from nltk.parse.corenlp import CoreNLPDependencyPars…

How to convert hex string to color image in python?

im new in programming so i have some question about converting string to color image.i have one data , it consists of Hex String, like a fff2f3..... i want to convert this file to png like this.i can c…

How to add values to a new column in pandas dataframe?

I want to create a new named column in a Pandas dataframe, insert first value into it, and then add another values to the same column:Something like:import pandasdf = pandas.DataFrame() df[New column].…

value error happens when using GridSearchCV

I am using GridSearchCV to do classification and my codes are:parameter_grid_SVM = {dual:[True,False],loss:["squared_hinge","hinge"],penalty:["l1","l2"] } clf = …

How to remove english text from arabic string in python?

I have an Arabic string with English text and punctuations. I need to filter Arabic text and I tried removing punctuations and English words using sting. However, I lost the spacing between Arabic word…