multiprocessing global variable updates not returned to parent

后端 未结 5 542
天命终不由人
天命终不由人 2020-11-22 10:44

I am trying to return values from subprocesses but these values are unfortunately unpicklable. So I used global variables in threads module with success but have not been ab

5条回答
  •  面向向阳花
    2020-11-22 11:30

    I use p.map() to spin off a number of processes to remote servers and print the results when they come back at unpredictable times:

    Servers=[...]
    from multiprocessing import Pool
    p=Pool(len(Servers))
    p.map(DoIndividualSummary, Servers)
    

    This worked fine if DoIndividualSummary used print for the results, but the overall result was in unpredictable order, which made interpretation difficult. I tried a number of approaches to use global variables but ran into problems. Finally, I succeeded with sqlite3.

    Before p.map(), open a sqlite connection and create a table:

    import sqlite3
    conn=sqlite3.connect('servers.db') # need conn for commit and close
    db=conn.cursor()
    try: db.execute('''drop table servers''')
    except: pass
    db.execute('''CREATE TABLE servers (server text, serverdetail text, readings     text)''')
    conn.commit()
    

    Then, when returning from DoIndividualSummary(), save the results into the table:

    db.execute('''INSERT INTO servers VALUES (?,?,?)''',         (server,serverdetail,readings))
    conn.commit()
    return
    

    After the map() statement, print the results:

    db.execute('''select * from servers order by server''')
    rows=db.fetchall()
    for server,serverdetail,readings in rows: print serverdetail,readings
    

    May seem like overkill but it was simpler for me than the recommended solutions.

提交回复
热议问题