Skip to content

Commit

Permalink
Rewrote multiprocessing test to use no local functions
Browse files Browse the repository at this point in the history
Makes it pass when when multiprocessing uses spawn to start processes (like under Mac OS).
  • Loading branch information
cfbolz committed Feb 5, 2022
1 parent 3554e05 commit 464553a
Showing 1 changed file with 21 additions and 20 deletions.
41 changes: 21 additions & 20 deletions tests/test_sync.py
Expand Up @@ -607,32 +607,33 @@ def sync_func():
assert asyncio.iscoroutinefunction(sync_to_async(sync_func))


async def async_process(queue):
queue.put(42)


def sync_process(queue):
"""Runs async_process synchronously"""
async_to_sync(async_process)(queue)


def fork_first():
"""Forks process before running sync_process"""
queue = multiprocessing.Queue()
fork = multiprocessing.Process(target=sync_process, args=[queue])
fork.start()
fork.join(3)
# Force cleanup in failed test case
if fork.is_alive():
fork.terminate()
return queue.get(True, 1)


@pytest.mark.asyncio
async def test_multiprocessing():
"""
Tests that a forked process can use async_to_sync without it looking for
the event loop from the parent process.
"""

test_queue = multiprocessing.Queue()

async def async_process():
test_queue.put(42)

def sync_process():
"""Runs async_process synchronously"""
async_to_sync(async_process)()

def fork_first():
"""Forks process before running sync_process"""
fork = multiprocessing.Process(target=sync_process)
fork.start()
fork.join(3)
# Force cleanup in failed test case
if fork.is_alive():
fork.terminate()
return test_queue.get(True, 1)

assert await sync_to_async(fork_first)() == 42


Expand Down

0 comments on commit 464553a

Please sign in to comment.