小编Mar*_*ark的帖子

python迭代字典

我有两个词典.我在另一个里面迭代它.我想在每次迭代内循环而不是从头开始时选择下一个项目.

dict1={'a':1 , 'b':2 , 'c':3}
dict2={'x':10, 'y':20, 'z':30}

for key,value in dict1:
   #do something
    for k,v in dict2:
          #do something
Run Code Online (Sandbox Code Playgroud)

当key ='a'时,它迭代内循环并根据该循环中的代码执行一些操作.假设它在dict2中选择的动作为'x'.现在,当我必须使用key ='b'进行迭代时,我希望内循环的迭代从'y'开始,因为'x'已被选中.

python

15
推荐指数
1
解决办法
2万
查看次数

Python Flask关闭事件处理程序

我正在使用Flask作为REST端点,它将一个应用程序请求添加到队列中.然后,队列由第二个线程使用.

server.py

def get_application():
    global app
    app.debug = True
    app.queue = client.Agent()
    app.queue.start()                                                                                                                                                                                                                
    return app

@app.route("/api/v1/test/", methods=["POST"])
def test():
     if request.method == "POST":
        try:
           #add the request parameters to queue
           app.queue.add_to_queue(req)
        except Exception:
            return "All the parameters must be provided" , 400
     return "", 200

     return "Resource not found",404
Run Code Online (Sandbox Code Playgroud)

client.py

class Agent(threading.Thread):

      def __init__(self):
          threading.Thread.__init__(self)
          self.active = True
          self.queue = Queue.Queue(0)


      def run(self):
           while self.active:
              req = self.queue.get()
              #do something


      def add_to_queue(self,request):
           self.queue.put(request)
Run Code Online (Sandbox Code Playgroud)

烧瓶中是否有关闭事件处理程序,以便每当关闭烧瓶应用程序时(例如重新启动apache服务时)我可以干净地关闭使用者线程?

python multithreading flask

12
推荐指数
1
解决办法
1万
查看次数

标签 统计

python ×2

flask ×1

multithreading ×1