| 67 | @pytest.mark.slow |
| 68 | @pytest.mark.subprocess |
| 69 | def test_multiprocess_close(dsn, tmpdir): |
| 70 | # Check the problem reported in psycopg2#829 |
| 71 | # Subprocess gcs the copy of the fd after fork so it closes connection. |
| 72 | module = f"""\ |
| 73 | import time |
| 74 | import psycopg |
| 75 | |
| 76 | def thread(): |
| 77 | conn = psycopg.connect({dsn!r}) |
| 78 | curs = conn.cursor() |
| 79 | for i in range(10): |
| 80 | curs.execute("select 1") |
| 81 | time.sleep(0.1) |
| 82 | |
| 83 | def process(): |
| 84 | time.sleep(0.2) |
| 85 | """ |
| 86 | |
| 87 | script = """\ |
| 88 | import time |
| 89 | import threading |
| 90 | import multiprocessing |
| 91 | import mptest |
| 92 | |
| 93 | t = threading.Thread(target=mptest.thread, name='mythread') |
| 94 | t.start() |
| 95 | time.sleep(0.2) |
| 96 | multiprocessing.Process(target=mptest.process, name='myprocess').start() |
| 97 | t.join() |
| 98 | """ |
| 99 | |
| 100 | with (tmpdir / "mptest.py").open("w") as f: |
| 101 | f.write(module) |
| 102 | env = dict(os.environ) |
| 103 | env["PYTHONPATH"] = str(tmpdir + os.pathsep + env.get("PYTHONPATH", "")) |
| 104 | out = sp.check_output( |
| 105 | [sys.executable, "-c", script], stderr=sp.STDOUT, env=env |
| 106 | ).decode("utf8", "replace") |
| 107 | assert out == "", out.strip().splitlines()[-1] |
| 108 | |
| 109 | |
| 110 | @pytest.mark.slow |