Skip to content Skip to sidebar Skip to footer

How To Run A Thread More Than Once In Python

I am trying to run a thread more than once and keep getting an error: RuntimeError: threads can only be started once I have tried reading up multithreading and implementing it in m

Solution 1:

Here is a working example (for python 2.7).

The program has two modes of operation:

  • with no arguments it runs the receive loop
  • with arguments it sends a datagram

Note how r.start() and r.terminate() are called outside of the while loop in client. Also, receive has a while True loop.

import sys
import socket
from multiprocessing import Process, Queue

UDP_ADDR = ("", 13000)

def send(m):
    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) 
    sock.sendto(m, UDP_ADDR)

def receive(q):
    buf = 1024
    Sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    Sock.bind(UDP_ADDR)
    while True:
      (data, addr) = Sock.recvfrom(buf)
      q.put(data)

def client():
  q = Queue()
  r = Process(target = receive, args=(q,))
  r.start()

  print "client loop started"
  while True:
      m = q.get()
      print "got:", m
      if m == "stop":
          break
  print "loop ended"

  r.terminate()

if __name__ == '__main__':
  args = sys.argv
  if len(args) > 1:
    send(args[1])
  else:
    client()

Solution 2:

I think the problem is once the thread is started, calling thread.start() again throws the error.

Using a try block would might work as a simple fix:

while True:
    try:
      r.start()
    except Exception:  
#or except RunTimeError: 
      pass
    if q.get() == "stop":
        print "Stopped"
        break
    print "Running program"

Post a Comment for "How To Run A Thread More Than Once In Python"