Skip to content

Instantly share code, notes, and snippets.

@Iftimie
Created September 6, 2020 12:02
Show Gist options
  • Save Iftimie/57f273ae19c2f8a92e462e8abfb93f8a to your computer and use it in GitHub Desktop.
Save Iftimie/57f273ae19c2f8a92e462e8abfb93f8a to your computer and use it in GitHub Desktop.
Add signaling between collector and triebuilder
class Collector(object):
def collect_phrase(self, phrase):
shared_path = "/app/assembler/collector/shared_phrases"
sorted_files = sorted(os.listdir(shared_path))
current_time = time.time()
seconds_30 = 30
if not sorted_files:
curfile = str(int(current_time))
elif int(sorted_files[-1])+seconds_30 < current_time:
curfile = str(int(current_time))
requests.post(f"http://assembler.triebuilder:4000/build_trie?phrase_file={int(sorted_files[-1])}")
else:
curfile = sorted_files[-1]
fullpath = os.path.join(shared_path, curfile)
with open(fullpath, 'a') as f:
f.write(phrase+'\n')
from trie import Trie
import pickle
import os
import falcon
import json
class BuildTrie(object):
def on_post(self, req, resp):
phrase_file = req.params['phrase_file']
shared_path = "/app/assembler/triebuilder/shared_phrases"
trie = Trie()
for phrase_file in sorted(os.listdir(shared_path)):
fullpath = os.path.join(shared_path, phrase_file)
with open(fullpath, 'r') as f:
for line in f:
trie.add_phrase(line)
trie_local_file_name = "/app/assembler/triebuilder/shared_data/trie.dat"
pickle.dump(trie, open(trie_local_file_name, "wb"))
resp.status = falcon.HTTP_200
app = falcon.API()
app.add_route('/build_trie', BuildTrie())
assembler.triebuilder:
build: ./assembler/triebuilder
ports:
- "4000"
volumes:
- ./assembler/triebuilder/main.py:/app/assembler/triebuilder/main.py
- ./shared/trie.py:/app/assembler/triebuilder/trie.py
- ./shared/shared_data:/app/assembler/triebuilder/shared_data
- ./shared/shared_phrases:/app/assembler/triebuilder/shared_phrases
command: gunicorn --chdir /app/assembler/triebuilder main:app -b 0.0.0.0:4000 --reload
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment