nworkers.cr
is an experimental library in Crystal programming language that allows you to change the number of worker threads at runtime.
See Crystal Forum : How to add command line options to specify the number of threads?
-
Add the dependency to your
shard.yml
:dependencies: nworkers: github: kojix2/nworkers.cr
-
Run
shards install
This is an experimental library. It is intended for use where the number of workers is changed immediately after startup and not changed afterwards. For example, in a command line tool, where the number of threads is set as an option.
NWorkers.set_worker(n : Int)
: Sets the number of worker threads ton
.NWorkers.add_worker(n : Int)
: Addsn
worker threads.NWorkers.remove_worker(n : Int)
: Removesn
worker threads.
Here's an example:
require "nworkers"
# set the number of workers to 4
NWorkers.set_worker(4)
# add 2 more workers
NWorkers.add_worker(2)
# remove 1 worker
NWorkers.remove_worker
- Crystal creates the number of workers specified by the environment variable ENV [“CRYSTAL_WORKERS”] only once at the beginning.
- See scheduler.cr.
- This library will add workers later in the same manner.
- The author has used this library to create several small command line tools to verify that parallel processing with additional workers actually works.
- However, that there is no guarantee that adding or removing workers after the fact will work correctly.
- The author does not have sufficient knowledge or experience to examine whether this method will work well for long-running processes such as web applications.
- Pull requests and comments are always welcome!
- Fork it (https://github.com/kojix2/nworkers.cr/fork)
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request
Contributions are welcome! Please feel free to submit a Pull Request if you find a bug or you want to propose a new feature.