Ralk
Ralk

Reputation: 461

Pool multiprocessing fail

I'm doing a quite simple example of parallel work using 'Pool' from 'multiprocessing'. What I want to do is to launch many SQL queries at the same time.

I have activated the WAL mode to allow multiple read/write operations at the same time.

def FUNC1():        
    conn = sqlite3.connect("DB.db")
    cur = conn.cursor()

    cur.execute(QUERY1)
    print "TEST"

    cur.close()  
    conn.close()
    return

def FUNC2():
    conn = sqlite3.connect("DB.db")
    cur = conn.cursor()

    cur.execute(QUERY2)

    cur.close()  
    conn.close()

    return

if __name__ == '__main__':

    conn = sqlite3.connect("DB.db")
    cur = conn.cursor()
    cur.execute('PRAGMA journal_mode=wal')
    pool = Pool(processes=2)

    pool.map_async(FUNC1,"")
    pool.map_async(FUNC2,"")


    cur.close()
    conn.close()

The terminal should show a print : 'TEST' that will show that the routine has do the FUNC1 operation. The problem is that it shows nothing.

Does anyone knows why?


EDIT

I've changed the code (thank you 'mata'!) and now the problem is that there is no parallel work.

Actually the query takes 3 seconds to execute and If I pass three times the same query the final time is 9...

Why ?

def Query(Query):

    conn = sqlite3.connect("DB.db")
    cur = conn.cursor()

    curOperations.execute(Query)

    cur.close()
    conn.close()

    return


if __name__ == '__main__':

    conn = sqlite3.connect("DB.db")
    cur = conn.cursor()

    cur.execute('PRAGMA journal_mode=wal')
    conn.commit()

    pool = Pool(processes=2)

    pool.map_async(Query,['QUERY1', 'QUERY2', 'QUERY3'])

    pool.close()
    pool.join()

    cur.close()
    conn.close()

Upvotes: 1

Views: 478

Answers (1)

mata
mata

Reputation: 69062

Pool.map_async() calls a function once with each element of the supplied iterable as argument. You provide an empty iterable (empty string), so the function is never called. Pool.apply_async() is probably what you are looking for.

Upvotes: 1

Related Questions