Home  >  Q&A  >  body text

python 多进程使用

假设:有个数据操作,处理100万条数据,每个数据的value+1。
如简单代码假设

for x in range(1,100):
    x++

从数据库获取100条了,然后想多进程去执行x++?
还是说多进程去数据库获取数据?
谢谢了

迷茫迷茫2741 days ago535

reply all(4)I'll reply

  • PHPz

    PHPz2017-04-18 10:35:22

    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:

    1). 如果你要用多进程去数据库获取数据(就算你用了mysql连接池,可以不怎么考虑数据库连接的io消耗),
    你每取一次数据,总要有一次查询吧, 完了以后,你还要把更新后的数据写入到数据库了, 又是一次数据库操作,
    想想这个消耗有多大?
    2). 数据库获取100万数据,然后想多进程去执行x++; 这种情况啊,只要计算机内存够(只有100万数据,基本是没问题的), 用python的进程池map一下,确实也是没什么问题

    reply
    0
  • 怪我咯

    怪我咯2017-04-18 10:35:22

    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.

    reply
    0
  • PHPz

    PHPz2017-04-18 10:35:22

    In fact, the best way to implement multi-processing in Python is to use multiprocessing中的map

    Example (Python 3):

    # f.py
    # 要对某个列表中每个元素都执行一次的function
    def f(x):
        return x + 1
    # main.py
    from multiprocessing import pool
    
    from f import f
    # 创建进程池
    p = pool.Pool(4)
    lst = range(100)
    # 使用多进程对整个列表进行计算
    print(p.map(f, lst))

    reply
    0
  • PHP中文网

    PHP中文网2017-04-18 10:35:22

    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

    reply
    0
  • Cancelreply