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

Fix process leak #1470

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions tqdm/asyncio.py
Expand Up @@ -10,6 +10,9 @@
import asyncio
from sys import version_info

from copy import copy
from multiprocessing import active_children

from .std import tqdm as std_tqdm

__author__ = {"github.com/": ["casperdcl"]}
Expand All @@ -24,6 +27,8 @@ def __init__(self, iterable=None, *args, **kwargs):
super(tqdm_asyncio, self).__init__(iterable, *args, **kwargs)
self.iterable_awaitable = False
if iterable is not None:
curr_processes = active_children()
iterable = copy(iterable)
if hasattr(iterable, "__anext__"):
self.iterable_next = iterable.__anext__
self.iterable_awaitable = True
Expand All @@ -32,6 +37,13 @@ def __init__(self, iterable=None, *args, **kwargs):
else:
self.iterable_iterator = iter(iterable)
self.iterable_next = self.iterable_iterator.__next__
new_processes = active_children()
# get only freshly spawned processes
processes_on_termination = [x for x in new_processes if x not in curr_processes]
# if we don't need anything from them, we can terminate immediately
# (Maybe it can cause some problems, but this solution is better than processes filling up memory)
for child in processes_on_termination:
child.terminate()

def __aiter__(self):
return self
Expand Down