From 51923dcf0dd38638183c444d608f94df60f0226d Mon Sep 17 00:00:00 2001 From: Jan Tuomi Date: Thu, 30 Mar 2017 22:26:42 +0300 Subject: Add print lock --- client.py | 13 +++++++++---- server.py | 1 + 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/client.py b/client.py index f89112c..d3ad41a 100755 --- a/client.py +++ b/client.py @@ -21,12 +21,17 @@ BUFFER_SIZE = 1024 DEBUG = False running = True +print_lock = threading.Lock() +def t_print(string): + with print_lock: + print(string) + def debug(string): if (DEBUG): - print("{} DEBUG: {}".format(datetime.now(), string)) + t_print("{} DEBUG: {}".format(datetime.now(), string)) def signal_handler(signal, frame): - print("Closing sockets and shutting down...") + t_print("Closing sockets and shutting down...") global running running = False disconnect() @@ -75,7 +80,7 @@ def listen_messages(): debug(body) if username != nick: timestamp = strftime("%H:%M:%S", gmtime()) - print("{} {}: {}".format(timestamp, username, body)) + t_print("{} {}: {}".format(timestamp, username, body)) host = input("Host [{}]: ".format(DEFAULT_HOST)) nick = input("Nickname [{}]: ".format(DEFAULT_NICK)) @@ -96,7 +101,7 @@ kuuntelija = threading.Thread(target=listen_messages, args=()) kuuntelija.start() #odotetaan kayttajan kirjoittavan viestin tai lopettavan ohjelman -quit-komennolla -print("Type '-quit' to exit.") +t_print("Type '-quit' to exit.") while running: syote = input() if syote == "-quit": diff --git a/server.py b/server.py index 9e8ccf6..5e216f4 100755 --- a/server.py +++ b/server.py @@ -12,6 +12,7 @@ PORT = 5000 DEBUG = True running = True + def debug(string): if (DEBUG): print("{} DEBUG: {}".format(datetime.now(), string)) -- cgit v1.3