Python Multi-threaded queue usage

Source: Internet
Author: User

Url_web, thread 2, queue, #线程1

#

#

#

#!/usr/bin/env python#! coding=utf-8#! by=xiaoheimport queue,threading,time,randomfrom moniitems import monimport  Urllib2from flask import flask,request,jsonqueuelock = threading. Lock () Class t1 (threading. Thread):     def __init__ (Self,t_name,queue):         threading. thread.__init__ (Self,name=t_name)         self.data = queue     def run (self):         #for  i in  range (5):        while true:             queuelock.acquire ()              if  self.data.empty ():                 print time.cTime (), Self.getname ()                  self.data.put (Mon (). Runallget ())                  time.sleep (1)                  queuelock.release ()              else:                 queuelock.release ()          #print  time.ctime (), Self.getname (), "End" Class t2 (threading. Thread):     def __init__ (Self,t_name,queue):         threading. thread.__init__ (Self,name=t_name)         self.data=queue     def run (self):         #for i in range (5):        while true:             queuelock.acquire ()              if not self.data.empty ():                 vai = self.data.get ()                   #print    Self.getname ()  , vai                 a=urllib2. Request ("http://reboot:8088",  json.dumps (vai),  {' Content-type ':  ' Application/json '})                  urllib2.urlopen (a)                  Queuelock.release () &NBsp;           else:                 queuelock.release () Def mai ():     queue = queue.queue (5)     tt1 = t1 (' Shou ', queue)      tt2 = t2 (' FA ', queue)     tt1.start ()      Tt2.start ()     tt1.join ()     tt2.join () if __name__== ' __main__ ':     mai ()


This article is from the "Small Box" blog, please be sure to keep this source http://zhangxiaohe.blog.51cto.com/7821029/1571305

Python Multi-threaded queue usage

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.