There is a question: If it is the data in the database, why not execute sql? This is much more efficient than multi-process? If you have to choose one of the two given, then consider:
First store the original data in the queue (queue), as a producer Then fetch the data from the queue, perform operations, and act as a consumer At this time, you can open multiple threads on the consumer (of course, if you handle the lock well, Producers can also do multi-threading)
while tmp_queue.empty() is not True:
x = tmp_queue.get()
x += 1
In the queue, if there are always elements, the thread will continue to operate.
You can directly write the two functions of data reading and data +1 into one operation, and then use multiple processes to operate. Just use the process pool to operate as mentioned above. Set the size of the process pool according to the number of your cpu cores. Since there is no memory sharing and direct communication between multiple processes, you can first use multiple processes to read all the data from the database, and then use multiple processes to perform val+1
There is a question: If it is the data in the database, why not execute sql? This is much more efficient than multi-process?
If you have to choose one of the two given, then consider:
First store the original data in the queue (queue), as a producer
Then fetch the data from the queue, perform operations, and act as a consumer
At this time, you can open multiple threads on the consumer (of course, if you handle the lock well, Producers can also do multi-threading)
In the queue, if there are always elements, the thread will continue to operate.
In fact, the best way to implement multi-processing in Python is to use
multiprocessing
中的map
Example (Python 3):
You can directly write the two functions of data reading and data +1 into one operation, and then use multiple processes to operate. Just use the process pool to operate as mentioned above. Set the size of the process pool according to the number of your cpu cores. Since there is no memory sharing and direct communication between multiple processes, you can first use multiple processes to read all the data from the database, and then use multiple processes to perform val+1