wkpk11235
wkpk11235

Reputation: 87

Run two while loops without tkinter or thread modules

How do I make two while loops run at the same time? Here is my code design.

while True:
    print(recieve_message()) #this waits for the message
    send_message(input()) #this also waits for the input

This will not work, because both codes do not run without waiting. So, I wanted them to run on separate loops, like this:

while True:
    print(recieve_message())
while True:
    send_message(input())

how do I make these codes run simultaneously?

recieve_message

and

send_message

uses the socket module.

Upvotes: 0

Views: 45

Answers (1)

gommb
gommb

Reputation: 1117

You can use multithreading:

import threading


def f1():
    while True:
        print(recieve_message()) #this waits for the message


def f2():
    while True:
        send_message(input()) #this also waits for the input

threading.Thread(target=f1).start()
threading.Thread(target=f2).start()

Upvotes: 2

Related Questions