Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create new eventloop to run coroutines #1055

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions test/test_iterdatapipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -1601,6 +1601,15 @@ def _helper(input_data, exp_res, async_fn, input_col=None, output_col=None, max_
output_col=1,
)

# Test multiple asyncio eventloops
dp1 = IterableWrapper(range(50))
dp1 = dp1.async_map_batches(_async_mul_ten, 16)
dp2 = IterableWrapper(range(50))
dp2 = dp2.async_map_batches(_async_mul_ten, 16)
for v1, v2, exp in zip(dp1, dp2, [i * 10 for i in range(50)]):
self.assertEqual(v1, exp)
self.assertEqual(v2, exp)


if __name__ == "__main__":
unittest.main()
13 changes: 10 additions & 3 deletions torchdata/datapipes/iter/transform/callable.py
Original file line number Diff line number Diff line change
Expand Up @@ -447,9 +447,16 @@ def __init__(
self.max_concurrency = max_concurrency

def __iter__(self):
for batch in self.source_datapipe:
new_batch = asyncio.run(self.processbatch(batch))
yield new_batch
policy = asyncio.get_event_loop_policy()
loop = policy.new_event_loop()
try:
for batch in self.source_datapipe:
policy.set_event_loop(loop)
new_batch = loop.run_until_complete(self.processbatch(batch))
yield new_batch
finally:
loop.run_until_complete(loop.shutdown_asyncgens())
loop.close()

async def processbatch(self, batch):
sem = asyncio.Semaphore(self.max_concurrency)
Expand Down