Skip to content

Commit 8320f09

Browse files
committed
[python] FastPySGI: Fix server worker count
1 parent 8d7b2ac commit 8320f09

2 files changed

Lines changed: 7 additions & 5 deletions

File tree

frameworks/fastpysgi-asgi/app.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
# -- Dataset and constants --------------------------------------------------------
1616

1717
CPU_COUNT = int(multiprocessing.cpu_count())
18+
WRK_COUNT = len(os.sched_getaffinity(0))
1819

1920
MIME_TYPES = {
2021
'.css' : 'text/css',
@@ -124,7 +125,7 @@ async def db_close():
124125
DATABASE_POOL = None
125126

126127
async def db_setup():
127-
global DATABASE_POOL, DATABASE_URL, CPU_COUNT
128+
global DATABASE_POOL, DATABASE_URL, WRK_COUNT
128129
await db_close()
129130
max_pool_size = 0
130131
'''
@@ -140,7 +141,7 @@ async def db_setup():
140141
pass
141142
if not max_connections:
142143
return
143-
max_pool_size = int(max_connections * 0.87 / CPU_COUNT) + 1
144+
max_pool_size = int(max_connections * 0.87 / WRK_COUNT) + 1
144145
'''
145146
try:
146147
DATABASE_POOL = await asyncpg.create_pool(
@@ -368,4 +369,4 @@ async def app(scope, receive, send):
368369
fastpysgi.server.read_buffer_size = 256*1024
369370
fastpysgi.server.backlog = 16*1024
370371
fastpysgi.server.loop_timeout = 1
371-
fastpysgi.run(app, host, port, workers = CPU_COUNT, loglevel = 0)
372+
fastpysgi.run(app, host, port, workers = WRK_COUNT, loglevel = 0)

frameworks/fastpysgi/app.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
# -- Dataset and constants --------------------------------------------------------
1616

1717
CPU_COUNT = int(multiprocessing.cpu_count())
18+
WRK_COUNT = len(os.sched_getaffinity(0))
1819

1920
MIME_TYPES = {
2021
'.css' : 'text/css',
@@ -118,7 +119,7 @@ def db_close():
118119
DATABASE_POOL = None
119120

120121
def db_setup():
121-
global DATABASE_POOL, DATABASE_URL, CPU_COUNT
122+
global DATABASE_POOL, DATABASE_URL, WRK_COUNT
122123
db_close()
123124
max_pool_size = 0
124125
try:
@@ -342,4 +343,4 @@ def app(env, start_response):
342343

343344
fastpysgi.server.read_buffer_size = READ_BUF_SIZE
344345
fastpysgi.server.backlog = 16*1024
345-
fastpysgi.run(app, host, port, workers = CPU_COUNT, loglevel = 0)
346+
fastpysgi.run(app, host, port, workers = WRK_COUNT, loglevel = 0)

0 commit comments

Comments
 (0)