non-blocking-spawn

A Crystal shard that spawns a fiber in any worker thread BUT the current one.

Purpose

When running Crystal with -Dpreview_mt sometimes fibers spawn in the current thread that might be blocked at that moment or for the duration of the run. This happens by design. Top level spawn has an option to force a fiber to spawn in the current thread (same_thread), but not one for the opposite (since it's probably not in high demand). That's what this shard adds.

It's a copy of the spawn method but when same_thread is false, it goes through all available worker threads, filters out the current one and picks one at random to spawn the fiber in.

While it might sound a bit useless, there are legit use cases:

WARNING

Installation

  1. Add the dependency to your shard.yml:
dependencies:
  non-blocking-spawn:
    github: GeopJr/non-blocking-spawn
  1. Run shards install

Usage

You can find non-blocking-spawn's docs on the sidebar.

require "non-blocking-spawn"

Non::Blocking.spawn do
  # expensive blocking code
end

# other expensive blocking code

Development

Contributing

  1. Read the Code of Conduct
  2. Fork it (https://github.com/GeopJr/non-blocking-spawn/fork)
  3. Create your feature branch (git checkout -b my-new-feature)
  4. Commit your changes (git commit -am 'Add some feature')
  5. Push to the branch (git push origin my-new-feature)
  6. Create a new Pull Request