简体   繁体   中英

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 my code without any luck.

Here is the function I am threading:

def receive(q):
    host = ""
    port = 13000
    buf = 1024
    addr = (host,port)
    Sock = socket(AF_INET, SOCK_DGRAM)
    Sock.bind(addr)
    (data, addr) = Sock.recvfrom(buf)
    q.put(data)

Here is the code I want to run:

q = Queue.Queue()
r = threading.Thread(target=receive, args=(q,))

while True:
    r.start()
    if q.get() == "stop":
        print "Stopped"
        break
    print "Running program"

When the stop message gets sent, the program should break out of the while loop, but it does not run due to multithreading. The while loop should constantly print out Running program , until the stop message is sent.

The queue is used to receive the variable data from the receive function (which is the stop ).

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()

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" 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM