python线程只能启动一次,如何在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()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值