How do I make my messager be able to send and receive messages at the same time? Currently It just is either sending or receiving.
You need a loop that handles both and not a while true loop per operation.
Just make sure that in the loop you execute receive, send, or sleep/wait.
yea… you would have to still give input every time it cycles though
Well. If you were doing this with sockets you could use the select module. But you are doing something else with databases. So the idea is the same, a loop to check if it needs to receive, if not it sends. But how, I would need to try to understand what you are doing.
ok, ill explain
messenger using replit db
def sound(): audio.play_file('notif.mp3') sound()
broken rn, also trying to fix this.
if not db.get('reading') == None: db['reading'] = "None"
if the new database is being first initalized, it sets the message to “None”. (no server)
inp = input("1. Watch\n2. Talk\n")
try: reading_c = db["c"] except: db['c'] = 0 reading_c = db["c"]
Gets an old copy of what
db['c'] is. This is for checking if a new message is received. Also sets c to 0 if its a new database. (no server)
if inp == "1": while True: if db['c'] != reading_c: print(db['reading']) reading_c = db['c'] sound() else: sleep(0.1)
Checks to see if a new message is sent by telling if the value of c was incremented. Then, prints
db['reading'] and resets old c (reading_c)
elif inp == "2": while True: sending = input(">> ") db['c'] += 1 db['reading'] = sending
gets input, increments c, sets
db['reading'] to the input. Later on i’ll add names.
You need to find a way to break the infinite loops. For example by using a message end that is recognised when receiving and is the last thing sent.
Btw sending and reading literally at the same time in python can be done but you need multithreaded programming (and even that in python is not truly at the same time). I would suggest to learn go for that type of fun!
since i am most fluent in python i like to sketch a high-level model of it out in python, then migrate to other languages. i was orginally going to port it to C, but go is another idea. Could you help with this? I am very poor at multi-threading. Also, that idea for breaking out is really nice except if someone wanted to stay watching or typing.
No, I cannot. I gave up python multithreaded and moved to other languages for when I need true concurrency. Maybe somebody else can help. Just realise it is fake concurrency, but I guess right now that is less important for you.
Maybe w3school can help you there
Will check that link out. Do you know anything about the sound issue? I even set audio to true in .replit.
I love fpesselo’s answer as that’s what I did in Wolf Chat but otherwise, I’d use asyncio with websockets
Still need to try sound on Replit, I am still busy with the logic of my hobby game still. Sorry…
It is okay, thank you for trying. I am also very stumped.
Btw this is a simple example of doing what you ask with threads.
import threading import time # function to simulate sending data def send_data(): while True: print("Sending data...") time.sleep(1) # function to simulate receiving data def receive_data(): while True: print("Receiving data...") time.sleep(1) # create and start two threads, one for sending and one for receiving send_thread = threading.Thread(target=send_data) receive_thread = threading.Thread(target=receive_data) send_thread.start() receive_thread.start() # wait for threads to finish send_thread.join() receive_thread.join()
You can find examples like this around (not my code but seemed easy enough)
you should use asyncio since more tasks can be created although each is slower
That would work, but learning multi-threading would be good as asyncio is good in specific situations.
In both cases python is slow (because it is not multi threading or multi process anyhow …)
i modified the code:
from replit import Database, audio from time import sleep from os import system import threading system('clear') def sound(): audio.play_file('notif.mp3') sound() db = Database("https://kv.replit.com/v0/eyJhbGciOiJIUzUxMiIsImlzcyI6ImNvbm1hbiIsImtpZCI6InByb2Q6MSIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJjb25tYW4iLCJleHAiOjE2ODA5Mjg3OTcsImlhdCI6MTY4MDgxNzE5NywiZGF0YWJhc2VfaWQiOiI5NDU1MTBmOC02ZmMwLTQ3ODgtODg1Ni1mYmI4ZjlkYmE5ZTQiLCJ1c2VyIjoiLWpwZyIsInNsdWciOiJmcmVldGV4dCJ9.KGfvPr02FgnDDqrLivG4veCzsQ3pKtks1hCQDg2iZBcmO6K8hAY_6n5cg7rYYGZbkHpuURyMvGHZs5fYqA0Gmw") # pls no bomb database ❤️ if not db.get('reading') == None: db['reading'] = "None" try: reading_c = db["c"] except: db['c'] = 0 reading_c = db["c"] system('clear') def receive(): global reading_c while True: if db['c'] != reading_c: print(db['reading']) reading_c = db['c'] sound() else: sleep(0.1) def send(): while True: sending = input(">> ") db['c'] += 1 db['reading'] = sending send_thread = threading.Thread(target=send) receive_thread = threading.Thread(target=receive) send_thread.start() receive_thread.start() send_thread.join() receive_thread.join()
however, it is not acting properly.