minimal-django: play with uwsgi mules

This commit is contained in:
Benjamin Dauvergne 2020-07-04 11:22:58 +02:00
parent eef15b1243
commit ecb6aed21f
4 changed files with 62 additions and 58 deletions

View File

@ -1,56 +0,0 @@
*** Starting uWSGI 2.0.18-debian (64bit) on [Fri Apr 10 23:25:15 2020] ***
compiled with version: 9.3.0 on 23 March 2020 14:44:29
os: Linux-5.2.0-2-amd64 #1 SMP Debian 5.2.9-2 (2019-08-21)
nodename: revestel
machine: x86_64
clock source: unix
pcre jit disabled
detected number of CPU cores: 4
current working directory: /home/bdauvergne/wd/eo/misc-bdauvergne/minimal-django
detected binary path: /usr/bin/uwsgi-core
*** WARNING: you are running uWSGI without its master process manager ***
your processes number limit is 29871
your memory page size is 4096 bytes
detected max file descriptor number: 1024
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uwsgi socket 0 bound to TCP address :3001 fd 3
Python version: 3.8.2 (default, Apr 1 2020, 15:52:55) [GCC 9.3.0]
*** Python threads support is disabled. You can enable it with --enable-threads ***
Python main interpreter initialized at 0x56427be0dc50
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 72920 bytes (71 KB) for 1 cores
*** Operational MODE: single process ***
WSGI app 0 (mountpoint='') ready in 1 seconds on interpreter 0x56427be0dc50 pid: 12899 (default app)
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI worker 1 (and the only) (pid: 12899, cores: 1)
*** Starting uWSGI 2.0.18-debian (64bit) on [Fri Apr 10 23:25:27 2020] ***
compiled with version: 9.3.0 on 23 March 2020 14:44:29
os: Linux-5.2.0-2-amd64 #1 SMP Debian 5.2.9-2 (2019-08-21)
nodename: revestel
machine: x86_64
clock source: unix
pcre jit disabled
detected number of CPU cores: 4
current working directory: /home/bdauvergne/wd/eo/misc-bdauvergne/minimal-django
detected binary path: /usr/bin/uwsgi-core
*** WARNING: you are running uWSGI without its master process manager ***
your processes number limit is 29871
your memory page size is 4096 bytes
detected max file descriptor number: 1024
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uwsgi socket 0 bound to TCP address :3001 fd 3
Python version: 3.8.2 (default, Apr 1 2020, 15:52:55) [GCC 9.3.0]
*** Python threads support is disabled. You can enable it with --enable-threads ***
Python main interpreter initialized at 0x56045df19c50
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 72920 bytes (71 KB) for 1 cores
*** Operational MODE: single process ***
WSGI app 0 (mountpoint='') ready in 1 seconds on interpreter 0x56045df19c50 pid: 12998 (default app)
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI worker 1 (and the only) (pid: 12998, cores: 1)
coucou
[pid: 12998|app: 0|req: 1/1] 127.0.0.1 () {38 vars in 766 bytes} [Fri Apr 10 16:25:30 2020] GET / => generated 7 bytes in 84 msecs (HTTP/1.1 200) 1 headers in 59 bytes (1 switches on core 0)

View File

@ -1,15 +1,20 @@
# minimal django application
import json
from django.urls import path
from django.http import HttpResponse
from django.core.wsgi import get_wsgi_application
import uwsgi
DEBUG = True
SECRET_KEY = 'a'
ROOT_URLCONF = 'app'
def home(request):
print('coucou')
uwsgi.mule_msg(json.dumps(('enqueue', 'work!')), 1)
return HttpResponse('Yoohoo!')
urlpatterns = [

55
minimal-django/mule.py Normal file
View File

@ -0,0 +1,55 @@
import json
import threading
import time
import uwsgi
WORKER_TIMEOUT = 60
queue = []
condition = threading.Condition()
def queue_worker():
while True:
with condition:
while not queue:
print('worker waiting!')
condition.wait(timeout=WORKER_TIMEOUT)
while queue:
item = queue.pop(0)
print('got item', item)
t = threading.Thread(target=queue_worker)
t.start()
def scheduler():
while True:
time.sleep(1)
with condition:
print('enqueue TICK!')
queue.append('tick!')
condition.notify()
t = threading.Thread(target=scheduler)
t.start()
def main_thread():
while True:
msg = uwsgi.mule_get_msg()
msg = json.loads(msg)
print('Got msg', repr(msg), msg)
kind, payload = msg
if kind == 'enqueue':
with condition:
queue.append(payload)
condition.notify_all()
print('enqueued payload', payload)
else:
print('unknown kind, payload:', kind, payload)
t = threading.Thread(target=main_thread)
t.daemon = True
t.start()

View File

@ -11,4 +11,4 @@ deps =
django
commands =
./install-uwsgi.sh
uwsgi-core --plugin python3 --http-socket :{env:PORT:3001} --wsgi-file app.py --logto app.log
uwsgi-core -T -M --plugin python3 --http-socket :{env:PORT:3001} --wsgi-file app.py --mule=mule.py