Skip to content

Instantly share code, notes, and snippets.

@gcavalcante8808
Created January 17, 2019 13:44
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 10 You must be signed in to fork a gist
  • Save gcavalcante8808/1f0a89ed72a3b3aca5dc63e2804eb896 to your computer and use it in GitHub Desktop.
Save gcavalcante8808/1f0a89ed72a3b3aca5dc63e2804eb896 to your computer and use it in GitHub Desktop.
Bjoern Django Final
import bjoern
import os, signal
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'app.settings')
app = get_wsgi_application()
NUM_WORKERS = 8
worker_pids = []
bjoern.listen(app, '0.0.0.0', 8000)
for _ in range(NUM_WORKERS):
pid = os.fork()
if pid > 0:
# in master
worker_pids.append(pid)
elif pid == 0:
# in worker
try:
bjoern.run()
except KeyboardInterrupt:
pass
exit()
try:
# Wait for the first worker to exit. They should never exit!
# Once first is dead, kill the others and exit with error code.
pid, xx = os.wait()
worker_pids.remove(pid)
finally:
for pid in worker_pids:
os.kill(pid, signal.SIGINT)
exit(1)
@arunthomaspuc
Copy link

Can you help me in how to run this on command line? Or in any deployment with nginx?

@gcavalcante8808
Copy link
Author

gcavalcante8808 commented Mar 3, 2021

Hi Friend, how can I help you with that? @arunthomaspuc

In time: Today I don't use this approach anymore as it is a vertical scaling approach (more threads/processes); Currently, I tend to scale horizontally through a high number of containers which only needs lines 1-14.

@arunthomaspuc
Copy link

Thank you so much for your fast reply.
Are you running with Bjoern on Kubernetes along with Django?
I wanted help in setting Bjoern up to serve my django application.

I mean after creating this wsgi_bjoern.py How or where do we invoke it?
Using gunicorn we use gunicorn mysite.wsgi bind 0.0.0.0:80
How do we invoke in case of Bjoern?

@gcavalcante8808
Copy link
Author

gcavalcante8808 commented Mar 3, 2021

Put wsgi_bjoern.py in your django application root, adjust line 7 and use python wsgi_bjoern.py to start your django app.

Are you running with Bjoern on Kubernetes along with Django? Yeah, just be sure to put and NGINX on front of django containers and that's it.

@phacic
Copy link

phacic commented Mar 26, 2021

ctrl+C doesn't close the server. The server only closes on container exit. Any help?

@gcavalcante8808
Copy link
Author

gcavalcante8808 commented Mar 28, 2021

@phacic Are u using it in a container?

If you're using it on a container, remember to add a tty: true to allow the KeyboardInterrupt to be passed to the script, otherwise use exec wsgi_bjoern.py as your entrypoint/cmd to maintain the same behavior.

@phacic
Copy link

phacic commented Mar 28, 2021

@gcavalcante8808 yes, am using it in a container.

Will give it a try and let you know the results. Thanks

@Skorpyon
Copy link

Amazing ))) 17K PRS on M1 laptop using Bjoern and 400RPS with 50% of "connection interrupt" and timeouts with Gunicorn )))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment