在Django中,我编写了一个名为update的自定义命令。 shell脚本不断调用此命令并更新数据库中的某些值。更新以线程完成。我把类与所有线程相关的东西放在与自定义命令相同的模块中。
在我的生产服务器上,当我尝试运行该shell脚本时,当我尝试访问我的模型时出现错误:
antennas = Antenna.objects.all()
错误是:
AttributeError: 'NoneType' object has no attribute 'objects'
然而,正如您所看到的,我确实在该文件中导入了app.models.Antenna。 所以对我来说,似乎整个网站的引用在某种程度上“丢失”在线程类中。
(我试图在这里删除所有非基本代码,因为它会使所有内容混乱,但我保持导入完整)
from django.core.management.base import NoArgsCommand, CommandError
from django.utils import timezone
from datetime import datetime, timedelta
from decimal import *
from django.conf import settings
from _geo import *
import random, time, sys, traceback, threading, Queue
from django.db import IntegrityError, connection
from app.models import Bike, Antenna
class Command(NoArgsCommand):
def handle_noargs(self, **options):
queue = Queue.Queue()
threadnum = 2
for bike in Bike.objects.filter(state__idle = False):
queue.put(bike)
for i in range(threadnum):
u = UpdateThread(queue)
u.setDaemon(True)
u.start()
queue.join()
return
class UpdateThread(threading.Thread):
def init(self, queue):
threading.Thread.init(self)
self.queue = queue
def run(self):
antennas = Antenna.objects.all()
while not self.queue.empty():
try:
[...]
except Exception:
traceback.print_exc(file=sys.stdout)
finally:
self.queue.task_done()
当我在UpdateThread类中尝试
from app.models import Antenna
时,出现错误:
ImportError: No module named app.models
该网站在网络服务器上运行良好。那里没有与模型相关的问题,只有当我在线程中做任何事情时 - 所有app.models-imports都失败了。 另外,在我看来,正好是相同的配置(我正在使用git)确实在另一台机器上运行,该机器运行相同的操作系统(debian wheezy,python 2.7) .3rc2& django 1.4.2)。
很可能我错过了一些关于穿线的明显内容,但我现在已经停留了太长时间。非常感谢您的帮助!
PS:我确实检查了循环导入 - 这些都不应该发生,因为我可以在其他(管理)类中使用我的模型,对吗?