-
Notifications
You must be signed in to change notification settings - Fork 12
/
sender.py
48 lines (40 loc) · 1.31 KB
/
sender.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# -*- coding: utf-8 -*-
import requests
import json
from multiprocessing import Pool
import time
import os
def send(data, url, key):
print 'send', data
try:
headers = {'Content-type': 'application/json', 'Accept': 'text/plain', 'X-PYTILT-KEY': key}
r = requests.post(url, data=json.dumps(data), headers=headers)
return r.status_code == 200
except requests.exceptions.RequestException:
return False
class Sender(object):
def __init__(self, batch_size=1):
self.queue = []
self.sending = []
self.batch_size = batch_size
self.url = os.environ.get('PYTILT_URL', None)
self.key = os.environ.get('PYTILT_KEY', None)
def add_data(self, data):
self.queue.append(data)
if len(self.queue) >= self.batch_size:
self.send()
def send(self):
pool = Pool(processes=1)
self.sending = list(self.queue)
self.queue = []
result = pool.apply_async(send, args=[self.sending, self.url, self.key], callback=self.completed)
pool.close()
pool.join()
def completed(self, was_sent):
if was_sent:
self.sending = []
else:
print 'send failed'
if len(self.queue) > 100:
self.queue = []
self.queue += self.sending