Multi-core programming method to share specific elements in Python

Asked 2 years ago, Updated 2 years ago, 41 views

I read the initial values from DB, and I'm holding them from Python to a tuple.

In this situation, you have to do a large number of operations by changing the value of a variable. The initial value tuple is read only and does not write the results every time. Only compare the results in different situations to keep the best value alive.

If you create an independent process (from multiprocessing import process), the problem is that for each multi-process, you have to read the value from the DB every time, or you have to read the data file that dropped the value from the DB somewhere. Continue.

If you create a multi-thread... It's GIL

Because we're going to change the value of a variable so many times that we're going to list the results per variable and compare them, and since they don't affect each other, it's obvious that multi-core programming will dramatically improve performance, but I don't know. The number of initial values is never small, as about 100 mixed with integers and floating points.

Please give me some advice.

python multicore

2022-09-22 19:29

2 Answers

If you use the extension module, do not use gil. Of course it is because it is C.

If you create a function with Python, make it without a Python code and declare nogil, it will be generated as a pure c code, so you can get out of gil.

In other words, if you use a function created by nogil as a Python thread, you use all the cpu cores.


2022-09-22 19:29

Why don't you try something like redis?


2022-09-22 19:29

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.