python线程按照顺序执行,Python按顺序执行线程

I have the following code:

import threading

def send_to_server(lst):

#Some logic to send the list to the server.

while 1:

lst = []

for i in range(1000):

lst.append(i)

task = threading.Thread(target=send_to_server,args(copy(lst),))

task.start()

I have a few Questions:

1) The idea for using threads is because sending to server takes time and I want to continue

generating the data without any stop.

The problem with this code is that if I created thread #3 and its taking long time to process,

By that time thread #4 will be started.

I want to assure that every list will be sent to server by the other I created it, means thread #3 will send to server the data before thread #4.

I understand that I need to use a Queue, but I don't know exactly how.

2)Should I use copy of lst? or I can use lst as well, I'm not sure.

解决方案

You want to use Queue, the threadsafe queue class in python. I imagine you want a thread to put things in the queue, and a thread to act on them serially like so:

q = Queue.Queue()

t1 = threading.Thread(target=fill_q,args(q, lst))

t2 = threading.Thread(target=consume_q,args(q, lst))

t1.start()

t2.start()

def fill_q(q, lst):

for elem in lst:

q.put(elem)

def consume_q(q, lst):

for i in range(len(lst)):

send_to_server(q.get())

If you are interested in performance, you may want to read up on the GIL in python, here https://wiki.python.org/moin/GlobalInterpreterLock

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值