{"id":17200528,"url":"https://github.com/toy/in_threads","last_synced_at":"2025-03-17T15:11:51.297Z","repository":{"id":669578,"uuid":"312851","full_name":"toy/in_threads","owner":"toy","description":"Run all possible enumerable methods in concurrent/parallel threads","archived":false,"fork":false,"pushed_at":"2024-01-28T11:25:45.000Z","size":177,"stargazers_count":37,"open_issues_count":1,"forks_count":0,"subscribers_count":7,"default_branch":"main","last_synced_at":"2024-04-29T17:49:50.121Z","etag":null,"topics":["concurrent","enumerable","parallel","ruby","threads"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/toy.png","metadata":{"files":{"readme":"README.markdown","changelog":"CHANGELOG.markdown","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2009-09-21T02:46:02.000Z","updated_at":"2024-05-30T23:06:09.170Z","dependencies_parsed_at":"2024-01-28T12:46:44.282Z","dependency_job_id":null,"html_url":"https://github.com/toy/in_threads","commit_stats":{"total_commits":300,"total_committers":3,"mean_commits":100.0,"dds":0.06000000000000005,"last_synced_commit":"f14d32e2186ff05a251c6a6c5bec0b818bc360b6"},"previous_names":[],"tags_count":20,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/toy%2Fin_threads","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/toy%2Fin_threads/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/toy%2Fin_threads/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/toy%2Fin_threads/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/toy","download_url":"https://codeload.github.com/toy/in_threads/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244056425,"owners_count":20390719,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["concurrent","enumerable","parallel","ruby","threads"],"created_at":"2024-10-15T02:08:56.803Z","updated_at":"2025-03-17T15:11:51.269Z","avatar_url":"https://github.com/toy.png","language":"Ruby","readme":"[![Gem Version](https://img.shields.io/gem/v/in_threads?logo=rubygems)](https://rubygems.org/gems/in_threads)\n[![Check](https://img.shields.io/github/actions/workflow/status/toy/in_threads/check.yml?label=check\u0026logo=github)](https://github.com/toy/in_threads/actions/workflows/check.yml)\n[![Rubocop](https://img.shields.io/github/actions/workflow/status/toy/in_threads/rubocop.yml?label=rubocop\u0026logo=rubocop)](https://github.com/toy/in_threads/actions/workflows/rubocop.yml)\n[![Code Climate](https://img.shields.io/codeclimate/maintainability/toy/in_threads?logo=codeclimate)](https://codeclimate.com/github/toy/in_threads)\n[![Depfu](https://img.shields.io/depfu/toy/in_threads)](https://depfu.com/github/toy/in_threads)\n[![Inch CI](https://inch-ci.org/github/toy/in_threads.svg?branch=master)](https://inch-ci.org/github/toy/in_threads)\n\n# in_threads\n\nRun all possible enumerable methods in concurrent/parallel threads.\n\n```ruby\nurls.in_threads(20).map do |url|\n  HTTP.get(url)\nend\n```\n\n## Installation\n\nAdd the gem to your Gemfile...\n\n```ruby\ngem 'in_threads'\n```\n\n...and install it with [Bundler](http://bundler.io).\n\n```sh\nbundle install\n```\n\nOr install globally:\n\n```sh\ngem install in_threads\n```\n\n## Usage\n\nLet's say you have a list of web pages to download.\n\n```ruby\nurls = [\n  \"https://google.com\",\n  \"https://en.wikipedia.org/wiki/Ruby\",\n  \"https://news.ycombinator.com\",\n  \"https://github.com/trending\"\n]\n```\n\nYou can easily download each web page one after the other.\n\n```ruby\nurls.each do |url|\n  HTTP.get(url)\nend\n```\n\nHowever, this is slow, especially for a large number of web pages. Instead,\ndownload the web pages in parallel with `in_threads`.\n\n```ruby\nrequire 'in_threads'\n\nurls.in_threads.each do |url|\n  HTTP.get(url)\nend\n```\n\nBy calling `in_threads`, the each web page is downloaded in its own thread,\nreducing the time by almost 4x.\n\nBy default, no more than 10 threads run at any one time. However, this can be\neasily overriden.\n\n```ruby\n# Read all XML files in a directory\nDir['*.xml'].in_threads(100).each do |file|\n  File.read(file)\nend\n```\n\nPredicate methods (methods that return `true` or `false` for each object in a\ncollection) are particularly well suited for use with `in_threads`.\n\n```ruby\n# Are all URLs valid?\nurls.in_threads.all? { |url| HTTP.get(url).status == 200 }\n\n# Are any URLs invalid?\nurls.in_threads.any? { |url| HTTP.get(url).status == 404 }\n```\n\n### Compatibility\n\nAll methods of `Enumerable` with a block can be used if block calls are evaluated independently, so following will\n\n`all?`, `any?`, `collect_concat`, `collect`, `count`, `cycle`, `detect`, `drop_while`, `each_cons`, `each_entry`,\n`each_slice`, `each_with_index`, `each_with_object`, `each`, `enum_cons`, `enum_slice`, `enum_with_index`,\n`filter_map`, `filter`, `find_all`, `find_index`, `find`, `flat_map`, `group_by`, `map`, `max_by`, `min_by`,\n`minmax_by`, `none?`, `one?`, `partition`, `reject`, `reverse_each`, `select`, `sort_by`, `sum`, `take_while`, `to_h`,\n`to_set`, `uniq`, `zip`.\n\nFollowing either don't accept block (like `first`), depend on previous block evaluation (like `inject`) or return an enumerator (like `chunk`), so will simply act as if `in_threads` wasn't used:\n\n`chain`, `chunk_while`, `chunk`, `compact`, `drop`, `entries`, `first`, `include?`, `inject`, `lazy`, `max`, `member?`,\n`minmax`, `min`, `reduce`, `slice_after`, `slice_before`, `slice_when`, `sort`, `take`, `tally`, `to_a`.\n\n### Break and exceptions\n\nExceptions are caught and re-thrown after allowing blocks that are still running to finish.\n\n**IMPORTANT**: only the first encountered exception is propagated, so it is recommended to handle exceptions in the block.\n\n`break` is handled in ruby \u003e= 1.9 and should be handled in jruby [9.1 after 9.1.9.0](https://github.com/jruby/jruby/issues/4697) and [9.2 and 9.3 after #7009](https://github.com/jruby/jruby/issues/7009). Handling is done in special way: as blocks are run outside of original context, calls to `break` cause `LocalJumpError` which is caught and its result is returned.\n\n## Copyright\n\nCopyright (c) 2009-2022 Ivan Kuchin. See LICENSE.txt for details.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftoy%2Fin_threads","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftoy%2Fin_threads","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftoy%2Fin_threads/lists"}