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

Unobtrusively modify Server#run to optionally execute in Thread#current. #111

Closed
wants to merge 1 commit into from
Closed

Conversation

kenkeiter
Copy link

(Sorry for the double pull-request. Something odd going on with my machine.)

This patch unobtrusively fixes Server#run so that it can be executed in the main thread. This is primarily to allow users to wrap run in whatever thread spawning mechanism they'd like to, and enables use with Celluloid by including the Celluloid module at load time,

Puma::Server.send(:include, Celluloid)

allowing supervision of the server's runloop. Note that it does NOT change the way run needs to be called, but simply allows it to be called with an additional hash arg :in_thread => false, which will start the event loop in the calling thread. For example:

server = Puma::Server.new(App)
server.add_tcp_listener('localhost', 9090)
server.run(:in_thread => false) # starts runloop in Thread#current, returning nothing.

It may also be started the old way, with no modification:

server.run

Allows implementation of custom threading mechanisms, or use with Celluloid.
@travisbot
Copy link

This pull request fails (merged 01ff40f into ca21111).

@tarcieri
Copy link
Contributor

Just a note on the example code here: I'm not really sure it's wise to try to force Celluloid upon code that wasn't really designed to use it. Celluloid will work just fine side-by-side with other multithreaded Ruby code, so there's no reason why you can't write your code with Celluloid and let Puma manage its own threads.

The Rack middleware API also makes it quite easy to use up all of the available stack space on YARV fibers as the stacks are quite small, and Celluloid presently runs all methods in fibers (the next release will let you opt out of this for certain methods). So if you do try to patch Celluloid in like this, you'll definitely want to avoid using Rack middleware if possible.

@kenkeiter
Copy link
Author

@tarcieri Good points, all of them. I'm simply looking for supervision chains, since it's a long-running process. I wish that Celluloid had a way to supervise existing threads because having to patch Puma and include Celluloid is sub-optimal.

At the same time, I do find it a bit odd that Puma starts its runloop in another thread by default.

@evanphx evanphx closed this in 39a06e7 Jul 19, 2012
aleksei-burlakov pushed a commit to aleksei-burlakov/puma that referenced this pull request Sep 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants