tests/multiprocess.py
changeset 78 64c62ac8f65d
parent 25 20a72a9a2d09
child 104 d8ff52a0390f
equal deleted inserted replaced
77:2cfef775f518 78:64c62ac8f65d
     7 and continue working with pgmanager in children processes. You can call create_conn again
     7 and continue working with pgmanager in children processes. You can call create_conn again
     8 in children, but new connection credentials will be accessible only from process
     8 in children, but new connection credentials will be accessible only from process
     9 where it was created -- no sharing between processes.
     9 where it was created -- no sharing between processes.
    10 
    10 
    11 get_conn and cursor will still work after fork. Connections from parent process will
    11 get_conn and cursor will still work after fork. Connections from parent process will
    12 be forgotten, that cannot be used un child process. 
    12 be forgotten, that cannot be used un child process.
    13 
    13 
    14 Connections cannot be shared between processes, DO NOT:
    14 Connections cannot be shared between processes, DO NOT:
    15  * get_conn, then fork, then put_conn
    15  * get_conn, then fork, then put_conn
    16  * get_conn, then fork, then use the connection, then .close()
    16  * get_conn, then fork, then use the connection, then .close()
    17 
    17 
    23 '''
    23 '''
    24 
    24 
    25 
    25 
    26 import multiprocessing
    26 import multiprocessing
    27 
    27 
    28 from config import Config
    28 from tests.config import Config
    29 from pgtoolkit import pgmanager
    29 from pgtoolkit import pgmanager
    30 
    30 
    31 
    31 
    32 def sub1(id):
    32 def sub1(id):
    33     with pgm.cursor() as curs:
    33     with pgm.cursor() as curs:
    50 cfg = Config('tests.conf')
    50 cfg = Config('tests.conf')
    51 pgm = pgmanager.get_instance()
    51 pgm = pgmanager.get_instance()
    52 pgm.create_conn(**cfg)
    52 pgm.create_conn(**cfg)
    53 
    53 
    54 with pgm.cursor() as curs:
    54 with pgm.cursor() as curs:
    55     print('[%d] pgconn: %d' % (multiprocessing.current_process().pid, curs.connection.get_backend_pid()))    
    55     print('[%d] pgconn: %d' % (multiprocessing.current_process().pid, curs.connection.get_backend_pid()))
    56     print('[%d] insert' % multiprocessing.current_process().pid)
    56     print('[%d] insert' % multiprocessing.current_process().pid)
    57     curs.execute('''INSERT INTO test (name) VALUES ('multi') RETURNING id''')
    57     curs.execute('''INSERT INTO test (name) VALUES ('multi') RETURNING id''')
    58     id = curs.fetchone()[0]
    58     id = curs.fetchone()[0]
    59     curs.connection.commit()
    59     curs.connection.commit()
    60     
    60 
    61     print('[%d] update' % multiprocessing.current_process().pid)
    61     print('[%d] update' % multiprocessing.current_process().pid)
    62     curs.execute('''UPDATE test SET name = 'multi-main' WHERE id=%s''', [id])
    62     curs.execute('''UPDATE test SET name = 'multi-main' WHERE id=%s''', [id])
    63     # not committed
    63     # not committed
    64 
    64 
    65 p1 = multiprocessing.Process(target=sub1, args=[id])
    65 p1 = multiprocessing.Process(target=sub1, args=[id])