0

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.

wkpk11235
  • 67
  • 6

1 Answers1

2

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()
gommb
  • 1,121
  • 1
  • 7
  • 21
  • 1
    process from multiprocessing does a similar thing too. The differences - https://stackoverflow.com/questions/3044580/multiprocessing-vs-threading-python – Jesse Oct 27 '17 at 04:02
  • This doesn't really work, since the computer waits for the input, then prints the output. Try it. EDIT: I found out it works in the cmd. Thanks! – wkpk11235 Oct 29 '17 at 06:40
  • @HyunsooKim Since this works, would you mind accepting it? – gommb Oct 29 '17 at 06:45