Hello everyone!
The last code that I was working on was very taxing and slow, so I decided to optimize it by using multiprocessing. When I did this, I discovered that somehow class variables that have been changed in code before calling ProcessPoolExecutor aren't taken into account when instantiating the processes. As an example, I prepared the following code:
If you run this code, the results are:
Class variable before pool: test.class_variable=1
test.class_variable=None
test.class_variable=None
test.class_variable=None
test.class_variable=None
test.class_variable=None
As I said at the start, even if we change the class variable before using multiprocessing they will be ignored and the code will use the original value (None in this case).
I wanted to know if someone knows a way to make the processes take into account the change in class variables.
The last code that I was working on was very taxing and slow, so I decided to optimize it by using multiprocessing. When I did this, I discovered that somehow class variables that have been changed in code before calling ProcessPoolExecutor aren't taken into account when instantiating the processes. As an example, I prepared the following code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import concurrent.futures class test(): class_variable = None def test_method( self , iter ): print (test.class_variable) if __name__ = = '__main__' : test.class_variable = 1 inst = test() print ( f "Class variable before pool: {test.class_variable=}" ) with concurrent.futures.ProcessPoolExecutor() as executor: executor. map (inst.test_method,[x for x in range ( 5 )]) |
Class variable before pool: test.class_variable=1
test.class_variable=None
test.class_variable=None
test.class_variable=None
test.class_variable=None
test.class_variable=None
As I said at the start, even if we change the class variable before using multiprocessing they will be ignored and the code will use the original value (None in this case).
I wanted to know if someone knows a way to make the processes take into account the change in class variables.