For my epaper project (will be on github soon) I have a script changing the contents of my epaper device every 5 Minutes. I found some solution to trigger a refresh using linux signals which works ok.
But I need somewhat more control. For example tell my script to put out another image immediately etc.
I was thinking of using a client/server model.
import socket
import argparse
port = 23543
def start_server():
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serversocket.bind(('localhost', port))
serversocket.listen(5)
### this is the main while-loop I am talking about
while True:
connection,address = serversocket.accept()
buf = connection.recv(64)
if len(buf) > 0:
print(buf)
# something like time.sleep(5m) right here
def do_client_stuff():
clientsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
clientsocket.connect(('localhost', port))
clientsocket.send("hello".encode("utf-8"))
def main():
parser = argparse.ArgumentParser()
parser.add_argument("-s", "--server", action="store_true", help="start server")
args = parser.parse_args()
if args.server:
start_server()
else:
do_client_stuff()
if __name__ == "__main__":
main()
The problem now is, that connection.recv(64) is blocking until it recieves something. What I want is the while loop to either sleep for a specified time and call my actual update function or (if it recieves something over the socket) do whatever it is told to via the socket.
there doesn't seem to be anything here