我想在将来可以扩展的Django应用程序中使用SQL Server数据库。
该应用程序将是I/O受限的,所以我认为使其可伸缩的最好方法是将Gunicorn与gevent worker类一起使用。
问题是,为了在--worker-class gevent中使用Gunicorn,Django应用程序应该与gevent兼容,并且通常会发生一些数据库客户端不兼容的情况(例如: mysqlclient lib)。
我找到了这个Django的SQL Server后端:https://pypi.org/project/django-mssql-backend/,但我不能理解它是否符合gevent (也就是“绿色”)。
我知道,如果它只使用标准的套接字/网络python库,就会出现这种情况,但我不能理解是不是这样。pyodbc SQL Server后端gevent是否兼容?
在官方手册和谷歌上,我找不到更多的信息。
发布于 2020-07-29 13:11:50
如果您愿意切换到Postgresql,我已经设置了一个模块,可以执行您需要的所有操作:
Python Postgres psycopg2 ThreadedConnectionPool exhausted
这包括一个用于处理数千个并发连接的连接池。
**更新**
我有一个使用Pymssql的解决方案:
import gevent.socket
import pymssql
import traceback
import sys
def wait_callback(read_fileno):
gevent.socket.wait_read(read_fileno)
pymssql.set_wait_callback(wait_callback)
def conn():
return pymssql.connect(server=server, user=user, password=password, autocommit=True)
def fetchone(SQL, *args):
with conn() as c:
with c.cursor() as cursor:
try:
cursor.execute(SQL, args)
except TypeError:
cursor.execute(SQL, args[0])
except Exception as exc:
print(sys._getframe().f_back.f_code)
print(sys._getframe().f_back.f_code.co_name)
traceback.print_exc()
return ()
return cursor.fetchone()
def fetchall(SQL, *args):
with conn() as c:
with c.cursor() as cursor:
try:
cursor.execute(SQL, args)
except TypeError:
cursor.execute(SQL, args[0])
except Exception as exc:
print(sys._getframe().f_back.f_code)
print(sys._getframe().f_back.f_code.co_name)
traceback.print_exc()
return ()
return cursor.fetchall()
def execute(PSQL, *args):
with conn() as c:
with c.cursor() as cursor:
try:
cursor.execute(PSQL, args)
except TypeError:
cursor.execute(PSQL, args[0])
except ValueError:
cursor.execute(PSQL, tuple(args[0]))
except:
print(sys._getframe().f_back.f_code)
print(sys._getframe().f_back.f_code.co_name)
traceback.print_exc()
return ()
finally:
return cursor.close()https://stackoverflow.com/questions/63035986
复制相似问题