本帖最后由 wangwei0511 于 2023-6-5 15:11 编辑
写了两段代码,这两段代码(query和dy)都是不断的轮询 数据库(间隔10s、20s),如果数据库有新数据,就执行相应的操作。
我用一个主程序用两个线程,启动了这两段代码,但是总是运行差不多十多个小时就报错,
我的问题是,怎么才能正常的一直循环运行下去?
报错内容如下:
[Asm] 纯文本查看 复制代码 Fatal Python error: none_dealloc: deallocating None: bug likely caused by a refcount error in a C extension
Python runtime state: initialized
Thread 0x00002fa0 (most recent call first):
File "g:\\u5bfc\u6e38\u76f8\u5173\u7a0b\u5e8f\\u5bfc\u6e38\u540e\u53f0\u6267\u884c\u7a0b\u5e8f\def\u67e5\u8be2v2.py", line 31 in dy_thread
File "G:\python\Lib\threading.py", line 975 in run
File "G:\python\Lib\threading.py", line 1038 in _bootstrap_inner
File "G:\python\Lib\threading.py", line 995 in _bootstrap
Current thread 0x00002b0c (most recent call first):
File "g:\\u5bfc\u6e38\u76f8\u5173\u7a0b\u5e8f\\u5bfc\u6e38\u540e\u53f0\u6267\u884c\u7a0b\u5e8f\def\u67e5\u8be2v2.py", line 15 in query_thread.py", line 15 in query_thread
File "G:\python\Lib\threading.py", line 975 in run
File "G:\python\Lib\threading.py", line 1038 in _bootstrap_inner
File "G:\python\Lib\threading.py", line 995 in _bootstrap
Thread 0x00000acc (most recent call first):
File "G:\python\Lib\threading.py", line 1132 in _wait_for_tstate_lock
File "G:\python\Lib\threading.py", line 1112 in join .py", line 41 in restart_program
File "g:\\u5bfc\u6e38\u76f8\u5173\u7a0b\u5e8f\\u5bfc\u6e38\u540e\u53f0\u6267\u884c\u7a0b\u5e8f\def\u67e5\u8be2v2.py", line 49 in <module>.py", line 41 in restart_program
File "g:\\u5bfc\u6e38\u76f8\u5173\u7a0b\u5e8f\\u5bfc\u6e38\u540e\u53f0\u6267\u884c\u7a0b\u5e8f\def\u67e5\u8be2v2.py", line 49 in <module>
代码如下:
[Python] 纯文本查看 复制代码 import threading
import time
import logging
from 导游查询v2 import query
from 导游录入智游宝 import dy
# 配置日志记录
logging.basicConfig(filename='log.txt', level=logging.INFO,
format='%(asctime)s %(levelname)s: %(message)s')
# 定义一个线程函数,用于执行 query() 函数
def query_thread():
while True:
try:
query()
except Exception as e:
logging.error(f"An error occurred: {e}")
# 发生异常时执行重启操作
restart_program()
time.sleep(10)
# 定义一个线程函数,用于执行 dy() 函数
def dy_thread():
while True:
try:
dy()
except Exception as e:
logging.error(f"An error occurred: {e}")
# 发生异常时执行重启操作
restart_program()
time.sleep(20)
# 创建两个线程并分别启动
t1 = threading.Thread(target=query_thread)
t2 = threading.Thread(target=dy_thread)
t1.start()
t2.start()
# 重启程序函数
def restart_program():
t1.join() # 等待线程 t1 结束
t2.join() # 等待线程 t2 结束
# 在这里执行重启操作,例如重新启动Python解释器或调用主程序的入口函数
# 主程序入口
if __name__ == "__main__":
while True:
time.sleep(60) # 每隔一段时间进行一次重启条件检查
restart_program()
|