当前位置: 首页 > news >正文

网站制作开发平台怎么做旅行网站

网站制作开发平台,怎么做旅行网站,西安 网站搭建,系统开发必须遵守的原则有哪些Python多线程编程中常用方法#xff1a; 1、join()方法#xff1a;如果一个线程或者在函数执行的过程中调用另一个线程#xff0c;并且希望待其完成操作后才能执行#xff0c;那么在调用线程的时就可以使用被调线程的join方法join([timeout]) timeout#xff1a;可选参数…Python多线程编程中常用方法 1、join()方法如果一个线程或者在函数执行的过程中调用另一个线程并且希望待其完成操作后才能执行那么在调用线程的时就可以使用被调线程的join方法join([timeout]) timeout可选参数线程运行的最长时间 2、isAlive()方法查看线程是否还在运行 3、getName()方法获得线程名 4、setDaemon()方法主线程退出时需要子线程随主线程退出则设置子线程的setDaemon() Python线程同步 1Thread的Lock和RLock实现简单的线程同步 import threading import time class mythread(threading.Thread):def __init__(self,threadname):threading.Thread.__init__(self,namethreadname)def run(self):global xlock.acquire()for i in range(3):x x1time.sleep(1)print xlock.release()if __name__ __main__:lock threading.RLock()t1 []for i in range(10):t mythread(str(i))t1.append(t)x 0for i in t1:i.start() 2使用条件变量保持线程同步 # codingutf-8 import threadingclass Producer(threading.Thread):def __init__(self,threadname):threading.Thread.__init__(self,namethreadname)def run(self):global xcon.acquire()if x 10000:con.wait() passelse:for i in range(10000):x x1con.notify()print xcon.release()class Consumer(threading.Thread):def __init__(self,threadname):threading.Thread.__init__(self,namethreadname)def run(self):global xcon.acquire()if x 0:con.wait()passelse:for i in range(10000):x x-1con.notify()print xcon.release()if __name__ __main__:con threading.Condition()x 0p Producer(Producer)c Consumer(Consumer)p.start()c.start()p.join()c.join()print x 3使用队列保持线程同步 # codingutf-8 import threading import Queue import time import randomclass Producer(threading.Thread):def __init__(self,threadname):threading.Thread.__init__(self,namethreadname)def run(self):global queuei random.randint(1,5)queue.put(i)print self.getName(), put %d to queue %(i)time.sleep(1)class Consumer(threading.Thread):def __init__(self,threadname):threading.Thread.__init__(self,namethreadname)def run(self):global queueitem queue.get()print self.getName(), get %d from queue %(item)time.sleep(1)if __name__ __main__:queue Queue.Queue()plist []clist []for i in range(3):p Producer(Producerstr(i))plist.append(p)for j in range(3):c Consumer(Consumerstr(j))clist.append(c)for pt in plist:pt.start()pt.join()for ct in clist:ct.start()ct.join() 生产者消费者模式的另一种实现 # codingutf-8 import time import threading import Queueclass Consumer(threading.Thread):def __init__(self, queue):threading.Thread.__init__(self)self._queue queuedef run(self):while True:# queue.get() blocks the current thread until an item is retrieved.msg self._queue.get()# Checks if the current message is the quitif isinstance(msg, str) and msg quit:# if so, exists the loopbreak# Processes (or in our case, prints) the queue itemprint Im a thread, and I received %s!! % msg# Always be friendly!print Bye byes!class Producer(threading.Thread):def __init__(self, queue):threading.Thread.__init__(self)self._queue queuedef run(self):# variable to keep track of when we startedstart_time time.time()# While under 5 seconds..while time.time() - start_time 5:# Produce a piece of work and stick it in the queue for the Consumer to processself._queue.put(something at %s % time.time())# Sleep a bit just to avoid an absurd number of messagestime.sleep(1)# This the quit message of killing a thread.self._queue.put(quit)if __name__ __main__:queue Queue.Queue()consumer Consumer(queue)consumer.start()producer1 Producer(queue)producer1.start() 使用线程池Thread pool同步队列Queue的实现方式 # A more realistic thread pool example # codingutf-8 import time import threading import Queue import urllib2 class Consumer(threading.Thread): def __init__(self, queue):threading.Thread.__init__(self)self._queue queue def run(self):while True: content self._queue.get() if isinstance(content, str) and content quit:breakresponse urllib2.urlopen(content)print Bye byes!def Producer():urls [http://www.python.org, http://www.yahoo.comhttp://www.scala.org, http://cn.bing.com# etc.. ]queue Queue.Queue()worker_threads build_worker_pool(queue, 4)start_time time.time()# Add the urls to processfor url in urls: queue.put(url) # Add the quit messagefor worker in worker_threads:queue.put(quit)for worker in worker_threads:worker.join()print Done! Time taken: {}.format(time.time() - start_time)def build_worker_pool(queue, size):workers []for _ in range(size):worker Consumer(queue)worker.start() workers.append(worker)return workersif __name__ __main__:Producer() 另一个使用线程池Map的实现 import urllib2 from multiprocessing.dummy import Pool as ThreadPool urls [http://www.python.org, http://www.python.org/about/,http://www.python.org/doc/,http://www.python.org/download/,http://www.python.org/community/]# Make the Pool of workers pool ThreadPool(4) # Open the urls in their own threads # and return the results results pool.map(urllib2.urlopen, urls) #close the pool and wait for the work to finish pool.close() pool.join()
http://www.w-s-a.com/news/466691/

相关文章:

  • 网站开发外包网站贵阳网站建设 网站制作
  • 官方微网站西安景观设计公司排行
  • 广州学做网站视频代做网站
  • 沈阳公司建站seo课程培训班
  • 杭州做微信网站软件公司网站建设毕业设计中期进度报告
  • 怎么做谷歌这样的网站如何建立一个网站放视频
  • 园区网站建设调研报告北京朝阳区哪里有网站开发
  • 网站角色权限wordpress 优化版
  • 购物网站ppt怎么做网络公司注册多少钱
  • 学做衣服上什么网站好贴吧高级搜索
  • 贵州 跨境电商网站建设做淘宝店铺有哪些好的网站
  • 广州正规网站制作公司网站搭建公司
  • ui设计零基础好学吗珠海网站建设优化推广
  • 网站开发多少费用火车头采集wordpress发布时间
  • 有没有做皮艺的网站教育培训网站建设ppt
  • 建设外贸商城网站制作如何建设景区旅游网站
  • 网站建设服务的具体条件怎么建设一个响应式网站
  • 做flash的网站wordpress设置前台投稿
  • 商务网站开发文档迅雷资源做下载网站
  • 无极磁铁网站如何把地图放到自己做的网站上
  • 青浦赵巷网站建设公司网站开发需求文档
  • 苏州网站建设的公司哪家好无锡网站制作那些
  • 装饰公司网站模板科技成果鉴定机构
  • 给公司做的东西放到私人网站上十堰为企业做网站的单位
  • 手机网站建设价钱手机自己做网站
  • 网站建设属于哪种公司电子商务查询网站
  • 工程建设标准强制性条文最新版本网站关键词排名优化应该怎么做
  • 网站网页设计内容品牌高端网站建设公司
  • 网站开发报价 福州中国建筑网官网手机版
  • 网站 图片 自动往右移专门做定制化的网站