使用django和apache启动一个新线程

时间:2014-12-29 00:39:01

标签: django multithreading apache

我正在使用django编写一个Web应用程序,其中一部分需要我下载谷歌图片搜索给出的图像并将其存储在服务器上的目录中。这是我写的代码:

def post(self, request, *args, **kwargs): image = request.FILES['image_file'] query = json.loads(request.POST['metadata']) owner = User.objects.filter(username="jane")[0] imageSearch = utils.GoogleImageSearch(query=query['query'], user=owner) instance = ModelInstance(user=owner, raw_image=image, metadata=str(query)) instance.save() someThread = utils.SomeThread(InstanceModel=instance, googleImageSearch=imageSearch) someThread.start() #This line does not start a new thread on apache someThread.join() return Response(status=status.HTTP_200_OK)

这是我的utils.py

class GoogleImageSearch(object): query = "" directoryToSave = "" numberOfImages = "" user = "" def initializeDirectoryStructure(self): pathToStoreImages = self.directoryToSave if os.path.exists(pathToStoreImages): shutil.rmtree(pathToStoreImages) os.makedirs(pathToStoreImages)

def __init__(self, query, user):
    self.query = query
    self.numberOfImages = constants.NUMBER_OF_SEARCH_RESULTS
    self.user = user
    self.directoryToSave = os.path.join(constants.IMAGE_CONTAINER_ROOT, user.username)
    self.initializeDirectoryStructure()

    def ImageSearch(self):
    exitCode, numberOfImages = imagesearch.searchAndSave(self.query, os.path.join(constants.IMAGE_CONTAINER_ROOT, self.user.username) , constant$

def __init__(self, query, user): self.query = query self.numberOfImages = constants.NUMBER_OF_SEARCH_RESULTS self.user = user self.directoryToSave = os.path.join(constants.IMAGE_CONTAINER_ROOT, user.username) self.initializeDirectoryStructure() def ImageSearch(self): exitCode, numberOfImages = imagesearch.searchAndSave(self.query, os.path.join(constants.IMAGE_CONTAINER_ROOT, self.user.username) , constant$

当我使用django开发服务器运行此代码时,它运行正常。 但是当我在Apache上运行它时,线程永远不会启动。

我猜测apache不允许从django应用程序创建新线程。 我想知道是否有任何方法(可能是一些apache配置文件更改)来启动线程。

提前致谢。

0 个答案:

没有答案