from core.mysql.models import TweetTable
from loguru import logger
import eventlet
import time
from eventlet import monkey_patch
monkey_patch()
def run():
q = TweetTable.select()
logger.debug(list(q))
if __name__ == '__main__':
while True:
g = eventlet.spawn(run)
g.wait()
time.sleep(1)
上面的代码是错误的
不打 monkey_patch,或者 monkey_patch 打晚了
都会导致连接复用
所以,我们要在一开始就打上 monkey_patch
from eventlet import monkey_patch
monkey_patch()
from core.mysql.models import TweetTable
from loguru import logger
import eventlet
import time
def run():
q = TweetTable.select()
logger.debug(list(q))
if __name__ == '__main__':
while True:
g = eventlet.spawn(run)
g.wait()
time.sleep(1)
把 monkey_patch 一到最开始,就可以了