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

wordpress 正在发送请求苹果aso优化

wordpress 正在发送请求,苹果aso优化,上海网站建设学校与管理中专,成都市网站建设公司Python多线程编程中常用方法: 1、join()方法:如果一个线程或者在函数执行的过程中调用另一个线程,并且希望待其完成操作后才能执行,那么在调用线程的时就可以使用被调线程的join方法join([timeout]) timeout:可选参数…

Python多线程编程中常用方法:

1、join()方法:如果一个线程或者在函数执行的过程中调用另一个线程,并且希望待其完成操作后才能执行,那么在调用线程的时就可以使用被调线程的join方法join([timeout]) timeout:可选参数,线程运行的最长时间

2、isAlive()方法:查看线程是否还在运行

3、getName()方法:获得线程名

4、setDaemon()方法:主线程退出时,需要子线程随主线程退出,则设置子线程的setDaemon()

Python线程同步:

(1)Thread的Lock和RLock实现简单的线程同步:

import threading
import time
class mythread(threading.Thread):def __init__(self,threadname):threading.Thread.__init__(self,name=threadname)def run(self):global xlock.acquire()for i in range(3):x = x+1time.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)使用条件变量保持线程同步:

# coding=utf-8
import threadingclass Producer(threading.Thread):def __init__(self,threadname):threading.Thread.__init__(self,name=threadname)def run(self):global xcon.acquire()if x == 10000:con.wait() passelse:for i in range(10000):x = x+1con.notify()print xcon.release()class Consumer(threading.Thread):def __init__(self,threadname):threading.Thread.__init__(self,name=threadname)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)使用队列保持线程同步:

# coding=utf-8
import threading
import Queue
import time
import randomclass Producer(threading.Thread):def __init__(self,threadname):threading.Thread.__init__(self,name=threadname)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,name=threadname)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('Producer'+str(i))plist.append(p)for j in range(3):c = Consumer('Consumer'+str(j))clist.append(c)for pt in plist:pt.start()pt.join()for ct in clist:ct.start()ct.join()

生产者消费者模式的另一种实现:

# coding=utf-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 "quit"if isinstance(msg, str) and msg == 'quit':# if so, exists the loopbreak# "Processes" (or in our case, prints) the queue itemprint "I'm 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
# coding=utf-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.com''http://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.hkea.cn/news/276816/

相关文章:

  • 医院网站制作优化关键词的方法有哪些
  • wordpress安装到网站吗泰安seo
  • 长春网站开发培训价格google play三件套
  • 做生存分析的网站有哪些国外新闻最新消息
  • 济南网站优化收费百度互联网营销
  • bootstrap响应网站模板下载发帖推广百度首页
  • 动态网站上的查询怎么做新媒体运营培训学校
  • 网站开发人员必备技能百度优化推广
  • 花都 网站建设百度推广怎么添加关键词
  • 开发公司成本部职责岗位职责和流程苏州网站建设优化
  • 湛江网站制作系统seo排名需要多少钱
  • 城乡现代社区建设seo关键词推广案例
  • 旅游网站开发外文文献关键洞察力
  • 大学生asp网站开发的实训周长沙百度快速优化
  • 黑龙江省建设网站百度投流运营
  • 网站关键词太多好不好兰州seo整站优化服务商
  • 义乌网站设计网店推广策划方案
  • 无锡网站优化工作室网站关键词排名优化推广软件
  • 长沙做网站的公司亚马逊seo什么意思
  • 仪征建设银行官方网站怎么优化一个网站
  • 那个网站可以查询美做空基金宁波网站推广平台效果好
  • 杨凌企业网站建设天津seo优化
  • 建设网站的工具免费b站在线观看人数在哪儿
  • 毕业设计餐饮网站建设国内前10电商代运营公司
  • 日本b2b网站市场调研的步骤
  • 强企网做网站网店推广有哪些
  • 博物馆网站建设策划书公司如何在百度宣传
  • 做cpa广告网站教程百度sem推广具体做什么
  • 免费网站建站WWW222国际军事最新消息今天
  • 做网站软件miscrosoft云服务器