用户访问http://example.com/url/并从page_parser
调用views.py
。 page_parser
从class Foo
创建script.py
的实例。
每次访问http://example.com/url/时,我都会看到内存使用率上升。我想垃圾收集器不会收集实例化的class Foo
。任何想法为什么会这样?
以下是代码:
views.py:
from django.http import HttpResponse
from script import Foo
from script import urls
# When user visits http://example.com/url/ I run `page_parser`
def page_parser(request):
Foo(urls)
return HttpResponse("alldone")
script.py:
import requests
from queue import Queue
from threading import Thread
class Newthread(Thread):
def __init__(self, queue, result):
Thread.__init__(self)
self.queue = queue
self.result = result
def run(self):
while True:
url = self.queue.get()
data = requests.get(url) # Download image at url
self.result.append(data)
self.queue.task_done()
class Foo:
def __init__(self, urls):
self.result = list()
self.queue = Queue()
self.startthreads()
for url in urls:
self.queue.put(url)
self.queue.join()
def startthreads(self):
for x in range(3):
worker = Newthread(queue=self.queue, result=self.result)
worker.daemon = True
worker.start()
urls = [
"https://static.pexels.com/photos/106399/pexels-photo-106399.jpeg",
"https://static.pexels.com/photos/164516/pexels-photo-164516.jpeg",
"https://static.pexels.com/photos/206172/pexels-photo-206172.jpeg",
"https://static.pexels.com/photos/32870/pexels-photo.jpg",
"https://static.pexels.com/photos/106399/pexels-photo-106399.jpeg",
"https://static.pexels.com/photos/164516/pexels-photo-164516.jpeg",
"https://static.pexels.com/photos/206172/pexels-photo-206172.jpeg",
"https://static.pexels.com/photos/32870/pexels-photo.jpg",
"https://static.pexels.com/photos/32870/pexels-photo.jpg",
"https://static.pexels.com/photos/106399/pexels-photo-106399.jpeg",
"https://static.pexels.com/photos/164516/pexels-photo-164516.jpeg",
"https://static.pexels.com/photos/206172/pexels-photo-206172.jpeg",
"https://static.pexels.com/photos/32870/pexels-photo.jpg"]
答案 0 :(得分:0)
涉及到几个活动部分,但我认为发生的事情如下:
Foo.queue
永远不会达到零(因为线程仍处于活动状态,等待新的队列项),因此无法进行垃圾回收所以你不断创建新的线程,新的Foo类,并且不能释放它们。
我不是queue.Queue的专家,但是如果你能看到WSGI进程中的线程数量每个请求3个(例如使用top(1)
),我的理论可以被验证。 / p>
作为旁注,这是您班级设计的副作用。你在__init__
中做了所有事情,这应该只是分配类属性。