Running waitress without threads

21 views
Skip to first unread message

Tom Hendrikx

unread,
May 22, 2025, 4:48:06 PMMay 22
to pylons-discuss
Hi,

For a work project I'm building Python software that runs inside a rather limited environment. An upstream software supplier provides software that spawns a Python environment where I need to execute code that is not thread-safe. The upstream supplier did add some mechanics to allow thread safety, but it requires me to call some custom code from each newly spawned thread.

 The actual functionality that I need to run in the environment is a small Flask app. When running this app using the Flask builtin dev server, it only works when I disable threads using: flask_app.run(threaded=False).

Now I wanted to not use the Flask dev server but something a little more sophisticated. Enter waitress: it runs on Windows (hard requirement), it's simple and it can serve my Flask app directly from Python (no external servers like apache+mod_wsgi required). There is only a single client sending requests to the server, so performance/concurrency is not a requirement.

However, I cannot get waitress to run as a single thread. Settings threads=1 doesn't help (it's probably the master process + 1 task thread). I found a SO post [1] that suggests two options, but both don't work: setting the parameter "threaded" to False, but this parameter doesn't exist (in waitress 3.0.2), and setting "connection_limit" to 1, which renders the app unreachable. 

Running threaded would probably mean that I subclass the ThreadedTaskDispatcher to add the specific code from the supplier. Another option would be to create a non-threading Dispatcher myself from scratch, and use that. All that seems way too complicated.

Do you have advice for me? Can I even run waitress without threads? Where did the "threaded" parameter to the serve() function go, if it ever existed? Should I be looking at a different Python-only app server that I somehow didn't find in my searches, and works on Windows? Should I even bother and just use the Flask dev server?


Thanks for all knowledge you might be able to share

Kind regards,
Tom

Jonathan Vanasco

unread,
May 22, 2025, 6:27:45 PMMay 22
to pylons-discuss
I can't speak to your Waitress questions, but uwsgi will build on windows with cygwin and can run multiple processes with single threads.  

Michael Merickel

unread,
May 22, 2025, 6:40:20 PMMay 22
to pylons-...@googlegroups.com
The stdlib offers wsgiref.simple_server [1] which is single-threaded. You gave a flask example but it'll work fine for any wsgi app.

The example on https://trypyramid.com which I'll copy here will work for any wsgi app including flask.

from wsgiref.simple_server import make_server from pyramid.config import Configurator from pyramid.response import Response def hello_world(request): return Response('Hello World!') if __name__ == '__main__': with Configurator() as config: config.add_route('hello', '/') config.add_view(hello_world, route_name='hello') app = config.make_wsgi_app() server = make_server('0.0.0.0', 6543, app) server.serve_forever()

Pyramid also exposes a wsgiref entry point if you are using PasteDeploy such that you can use it from your INI file like:

[server:main]
use = egg:pyramid#wsgiref
host = 0.0.0.0
port = 8080


-- 
You received this message because you are subscribed to the Google Groups "pylons-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pylons-discus...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/pylons-discuss/f1d3abc3-0f31-41b7-84dc-0febcf191228n%40googlegroups.com.

Tom Hendrikx

unread,
May 26, 2025, 7:10:31 AMMay 26
to pylons-discuss
Hi,

Thanks for your suggestions. I used wsgiref to implement a bare-bones server process. This is probably quite similar to the Flask dev server, but at least it doesn't emit a big fat warning about not using in production, which would make uninformed users uncomfortable. Since I'm running the server from the restricted environment, cygwin is not an option here, so no uwsgi.

Regards,
Tom

Chris McDonough

unread,
May 26, 2025, 9:46:55 AMMay 26
to pylons-...@googlegroups.com

> Hi,
> Thanks for your suggestions. I used wsgiref to implement a bare-bones server process. This is probably quite similar to the Flask dev server, but at least it doesn't emit a big fat warning about not using in production, which would make uninformed users uncomfortable. Since I'm running the server from the restricted environment, cygwin is not an option here, so no uwsgi.

For the record, although Waitress starts a thread when you set it to threads = 1, I doubt it means that the thread-unsafe library in the app you're writing would have an issue. The main thread of any WSGI server would never be accessing any of the resources accessed by the library, so the threads it spawned would be the only point of contention, and since there's one, there is no contention.

- C

Reply all
Reply to author
Forward
0 new messages