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

WSGI+ #1

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
WSGI+ v2
  • Loading branch information
Vitalii committed Feb 25, 2024
commit e64253df97d8657a6a4b882a43113b40160a2bd9
11 changes: 7 additions & 4 deletions examples/wsgi_plus.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import time
from concurrent.futures import Future
from threading import Thread


def application(environ, start_response, resume):
def application(environ, start_response):
def thread():
time.sleep(10)
resume()
time.sleep(3)
fut.set_result(3)

Thread(target=thread).start()
yield resume
fut = Future()
yield fut
body = b'Dzozdra!\n'
status = '200 OK'
headers = [('Content-type', 'text/plain')]
Expand Down
39 changes: 20 additions & 19 deletions gunicorn/workers/gthread.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import sys
import time
from collections import deque
from concurrent.futures import Future
from datetime import datetime
from functools import partial
from threading import RLock
Expand Down Expand Up @@ -115,20 +116,21 @@ def enqueue_req(self, conn):
conn.init()

def iterate():
gen = self.handle(conn, resume)
val = gen.send(None)
gen = self.handle(conn)
while True:
if skip := (val is resume):
yield ()
try:
val = gen.send(skip)
val = next(gen)
except StopIteration as e:
yield (e.value,)
yield e.value
if isinstance(val, Future):
val.add_done_callback(resume)
yield (), ()

iterator = iterate()

def resume():
fs = self.tpool.submit(next, iterator)
def resume(fs=None):
if fs is None or not fs.exception():
fs = self.tpool.submit(next, iterator)
self._wrap_future(fs, conn)

resume()
Expand Down Expand Up @@ -260,12 +262,11 @@ def finish_request(self, fs):
self.nr_conns -= 1
fs.conn.close()
return
if (result := fs.result()) is ():
return
else:
(result,) = result
try:
(keepalive, conn) = result
(keepalive, conn) = fs.result()
if keepalive == conn == ():
# the app was suspended
return
# if the connection should be kept alived add it
# to the eventloop and record it
if keepalive and self.alive:
Expand All @@ -289,7 +290,7 @@ def finish_request(self, fs):
self.nr_conns -= 1
fs.conn.close()

def handle(self, conn, resume):
def handle(self, conn):
keepalive = False
req = None
try:
Expand All @@ -298,7 +299,7 @@ def handle(self, conn, resume):
return (False, conn)

# handle the request
keepalive = yield from self.handle_request(req, conn, resume)
keepalive = yield from self.handle_request(req, conn)
if keepalive:
return (keepalive, conn)
except http.errors.NoMoreData as e:
Expand Down Expand Up @@ -329,7 +330,7 @@ def handle(self, conn, resume):

return (False, conn)

def handle_request(self, req, conn, resume):
def handle_request(self, req, conn):
environ = {}
resp = None
try:
Expand All @@ -350,14 +351,14 @@ def handle_request(self, req, conn, resume):
elif len(self._keep) >= self.max_keepalived:
resp.force_close()

respiter = self.wsgi(environ, resp.start_response, resume)
respiter = self.wsgi(environ, resp.start_response)
try:
if isinstance(respiter, environ['wsgi.file_wrapper']):
resp.write_file(respiter)
else:
for item in respiter:
skip = yield item
if not skip:
yield item
if not isinstance(item, Future):
resp.write(item)

resp.close()
Expand Down