无法在Django app中使用Python的threading.Thread

时间:2023-01-22 20:39:45

I am trying to create a web application as a front end to another Python app. I have the user enter data into a form, and upon submitting, the idea is for the data to be saved in a database, and for the data to be passed to a thread object class. The thread is something that is strictly kicked-off based on a user action. My problem is that I can import threading, but cannot access threading.Thread. When the thread ends, it will update the server, so when the user views the job information, they'll see the results.

我正在尝试创建一个Web应用程序作为另一个Python应用程序的前端。我让用户在表单中输入数据,并在提交时,想法是将数据保存在数据库中,并将数据传递给线程对象类。该线程是基于用户操作严格启动的。我的问题是我可以导入线程,但无法访问threading.Thread。当线程结束时,它将更新服务器,因此当用户查看作业信息时,他们将看到结果。

View:

视图:

@login_required(login_url='/login')
def createNetworkView(request):
    if request.method == "POST":
        # grab my variables from POST
        job = models.MyJob()
        # load my variables into MyJob object
        job.save()
        t = ProcessJobThread(job.id, my, various, POST, inputs, here)
        t.start()
        return HttpResponseRedirect("/viewJob?jobID=" + str(job.id))
    else:
        return HttpResponseRedirect("/")

My thread class:

我的线程类:

import threading # this works
print "About to make thread object" # This works, I see this in the log
class CreateNetworkThread(threading.Thread): # failure here
    def __init__(self, jobid, blah1, blah2, blah3):
        threading.Thread.__init__(self)

    def run(self):
        doCoolStuff()
        updateDB()

I get:

我明白了:

Exception Type: ImportError
Exception Value:    cannot import name Thread

However, if I run python on the command line, I can import threading and also do from threading import Thread. What's the deal?

但是,如果我在命令行上运行python,我可以导入线程,也可以从线程导入线程。这是怎么回事?

I have seen other things, like How to use thread in Django and Celery but that seemed overkill, and I don't see how that example could import threading and use threading.Thread, when I can't. Thank you.

我已经看到了其他的东西,比如如何在Django和Celery中使用线程,但这看起来有些过分,我不知道这个例子如何导入线程并使用线程。当我不能时,我就看不到。谢谢。

Edit: I'm using Django 1.4.1, Python 2.7.3, Ubuntu 12.10, SQLite for the DB, and I'm running the web application with ./manage.py runserver.

编辑:我正在使用Django 1.4.1,Python 2.7.3,Ubuntu 12.10,DBite for DB,我正在使用./manage.py runserver运行Web应用程序。

1 个解决方案

#1


0  

This was a silly issue I had. First, I had made a file called "threading.py" and someone suggest I delete it, which I did (or thought I did). The problem was because of me using Eclipse, the PyDev (Python) plugin for Eclipse only deleted the threading.py file I created, and hides the *.pyc file. I had a lingering threading.pyc file lingering around, even though PyDev has an option that I had enabled to delete orphaned .pyc files.

这是一个愚蠢的问题。首先,我创建了一个名为“threading.py”的文件,有人建议我删除它,我做了(或者我以为我做过)。问题是因为我使用Eclipse,Eclipse的PyDev(Python)插件只删除了我创建的threading.py文件,并隐藏了* .pyc文件。我有一个挥之不去的threading.pyc文件挥之不去,即使PyDev有一个选项,我已经启用删除孤立的.pyc文件。

#1


0  

This was a silly issue I had. First, I had made a file called "threading.py" and someone suggest I delete it, which I did (or thought I did). The problem was because of me using Eclipse, the PyDev (Python) plugin for Eclipse only deleted the threading.py file I created, and hides the *.pyc file. I had a lingering threading.pyc file lingering around, even though PyDev has an option that I had enabled to delete orphaned .pyc files.

这是一个愚蠢的问题。首先,我创建了一个名为“threading.py”的文件,有人建议我删除它,我做了(或者我以为我做过)。问题是因为我使用Eclipse,Eclipse的PyDev(Python)插件只删除了我创建的threading.py文件,并隐藏了* .pyc文件。我有一个挥之不去的threading.pyc文件挥之不去,即使PyDev有一个选项,我已经启用删除孤立的.pyc文件。