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 |
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]) |