forked from teamtachyon/Quillpad-Server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wordCounter.py
executable file
·51 lines (40 loc) · 1.31 KB
/
wordCounter.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
49
50
51
from flask import Flask, request, render_template, redirect, url_for
import itertools, threading, json
import time
from RingBuffers import RingBuffer, RingBufferFull
app = Flask(__name__)
lock = threading.Lock()
prevTime = time.time()
wordCount = itertools.count(int(open('wordcount').read()))
timerRingBuffer = RingBuffer(10, wordCount.next())
@app.route("/processInput")
def processInput():
action = request.args.get('action')
if action <> 'addWord':
return
count = wordCount.next()
if count % 100 == 0:
updateCount(count)
#print 'wordcount is: ', wordCount
currTime = int(time.time())
global prevTime
global timerRingBuffer
if currTime <> prevTime:
timerRingBuffer.append(wordCount)
prevTime = currTime
else:
timerRingBuffer.data[timerRingBuffer.get_curr()] = count
return ""
#print 'buffer is: ', timerRingBuffer.get()
@app.route("/processWordCounts")
def processWordCounts():
print 'buffer is: ', timerRingBuffer.get()
print 'response is', json.listToJSON(timerRingBuffer.get())
print
return json.listToJSON(timerRingBuffer.get())
def updateCount(count):
lock.acquire()
open('wordcount', 'w').write(str(count))
lock.release()
if __name__ == "__main__":
app.run(debug=True)