{"comment":{"created_at":"2011-10-19T17:09:59Z","body":"I tried a bunch of stuff for this. I thougth the most promising was:\r\n\r\n\r\n```python\r\ndef stream_iter(p):\r\n    for s in p.stdout:\r\n        yield s\r\n    p.stdout.close()\r\n\r\noutput = stream_iter(p)\r\n```\r\n\r\nwhich could work but then it has problems with some of the assumptions later on. \r\nAnd, since we never actually iterate over the stream in the above test-case, close will still not be called.\r\n\r\nSo, one could then make a class with a `__del__` method that closes the stream...\r\n\r\nNote that the BedTool class currently has no `__del__` method.","updated_at":"2011-10-19T17:09:59Z","url":"https://api.github.com/repos/daler/pybedtools/issues/comments/2458473","id":2458473,"user":{"avatar_url":"https://secure.gravatar.com/avatar/673a112cceaf8a4714ac6c3b6ae91690?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-140.png","gravatar_id":"673a112cceaf8a4714ac6c3b6ae91690","url":"https://api.github.com/users/brentp","id":1739,"login":"brentp"}},"action":"created","issue":{"number":41,"created_at":"2011-09-27T18:55:32Z","pull_request":{"diff_url":null,"patch_url":null,"html_url":null},"body":"Currently, closing streams in the `__del__` method of BedTools still leaves open file handles somewhere.  See this comment for details:\r\n\r\nhttps://github.com/daler/pybedtools/issues/38#issuecomment-2124557\r\n\r\nSpecifically, the following code should not return a \"too many files open\" error after a few thousand intersections:\r\n\r\n```python\r\nimport pybedtools\r\nimport sys\r\na = pybedtools.example_bedtool('a.bed')\r\nb = pybedtools.example_bedtool('b.bed')\r\nfor i in range(10000):\r\n    sys.stderr.write('\\r%s' % i)\r\n    sys.stderr.flush()\r\n    c = a.intersect(b, stream=True)\r\n    del c\r\n```\r\nNote, however, that this works fine (without the stream=True), suggesting that converting into an iterable is the issue:\r\n\r\n```python\r\nimport pybedtools\r\nimport sys\r\na = pybedtools.example_bedtool('a.bed')\r\nb = pybedtools.example_bedtool('b.bed')\r\nfor i in range(10000):\r\n    sys.stderr.write('\\r%s' % i)\r\n    sys.stderr.flush()\r\n    c = a.intersect(b)\r\n    del c\r\n```","comments":5,"title":"robustly close streams in \"BedTool.__del__\"","updated_at":"2011-10-19T17:09:59Z","url":"https://api.github.com/repos/daler/pybedtools/issues/41","id":1755208,"assignee":null,"milestone":null,"closed_at":null,"user":{"avatar_url":"https://secure.gravatar.com/avatar/f09a2bdc56610adbde7b8a860fa4d1f9?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-140.png","gravatar_id":"f09a2bdc56610adbde7b8a860fa4d1f9","url":"https://api.github.com/users/daler","id":115406,"login":"daler"},"html_url":"https://github.com/daler/pybedtools/issues/41","labels":[],"state":"open"},"legacy":{"issue_id":1755208,"comment_id":2458473}}