-
Notifications
You must be signed in to change notification settings - Fork 0
/
synchroniser.py
51 lines (43 loc) · 1.53 KB
/
synchroniser.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
import os
import time
import requests
from requests import RequestException
from config import SYNCHRONISATION_SERVER_URL, SYNCHRONISER_UPDATE_DELAY
LAST_SHM = b""
SHM_PATH = "/dev/shm/sys_seminar_shm"
while True:
UPDATE_SHM = False
if os.path.exists(SHM_PATH):
# Read shared memory file
with open(SHM_PATH, "rb") as f:
shm = f.read()
# If it has changes, post them to the synchronisation server
if shm != LAST_SHM:
try:
requests.post(SYNCHRONISATION_SERVER_URL, data=shm)
except RequestException as e:
print("Couldn't reach server or server error")
print(e)
else:
LAST_SHM = shm
print("Pushed shared memory update to server")
# If not, pull updates from the synchronisation server
else:
UPDATE_SHM = True
else:
UPDATE_SHM = True
if UPDATE_SHM:
# Pull updates from the synchronisation server
try:
new_shm = requests.get(SYNCHRONISATION_SERVER_URL).content
except RequestException as e:
print("Couldn't reach server or server error")
print(e)
else:
# If there are changes on the server, update local shared memory
if new_shm != LAST_SHM:
with open(SHM_PATH, "wb") as f:
f.write(new_shm)
LAST_SHM = new_shm
print("Updated shared memory from server")
time.sleep(SYNCHRONISER_UPDATE_DELAY)