from haystack.query import SearchQuerySet
sqs = SearchQuerySet().filter(content='Москва').order_by('-date')
for post in sqs:
print post.object.date
2013-08-03
2013-07-27
2013-07-20
2013-07-29
2013-07-29
2013-08-08
2013-08-04
2013-08-05
2013-08-04
2013-08-05
2013-08-05
2013-08-05
2013-08-05
2013-07-28
2013-08-04
2013-07-21
2013-07-14
2013-07-14
2013-07-07
2013-06-30
2013-06-23
2013-06-18
2013-06-09
2013-06-02
2013-05-26
2013-05-19
2013-05-12
2013-05-05
2013-04-28
2013-06-30
2013-07-10
2013-06-19
2013-06-12
2013-05-22
2013-05-15
2013-04-17
2013-07-13
2013-07-06
2013-06-29
2013-06-22
2013-06-15
2013-06-08
2013-06-01
2013-05-25
2013-05-18
2013-05-11
2013-05-04
2013-04-27
2013-04-20
2013-07-05
class ScanFriendsConsumer(WebsocketConsumer):
def connect(self):
self.accept()
def task():
t = threading.current_thread()
urls = [.... list with urls ....]
for counter, url in enumerate(urls, 1):
if getattr(t, "do_run", True):
response = requests.get(url).json()
self.thread = threading.Thread(target=task)
self.thread.start()
def disconnect(self, code):
print('socket disconnected')
self.thread.do_run = False
self.thread.join()
class MyConsumer(WebsocketConsumer):
def connect(self):
start = datetime.datetime.now()
self.accept()
print('socket opened')
urls = [.... list with urls ....]
for counter, url in enumerate(urls, 1): # Цикл, который нужно оборвать при отключении сокета
response = requests.get(url).json()
print(counter, response)
self.send(json.dumps({'progress': '{:.0f}%'.format(counter * 100 / len(urls)),
'data': response}))
print('Process time is: ', (datetime.datetime.now() - start).seconds)
self.close()
def disconnect(self, code):
print('socket closed')
# Тут нужно как-то отловить выполнение цикла и оборвать его выполнение