zoukankan      html  css  js  c++  java
  • Gevent中的同步与异步详解

      同步,异步概念

      1.同步就是发生调用时,一定等待结果返回,整个调用才结束;

      2.异步就是发生调用后,立即返回,不等待结果返回。被调用者通过状态、通知来通知调用者,或通过回调函数处理这个调用。

      查询

      1.同步查询

      2.异步查询

      同步异步与阻塞,非阻塞区别

      1.阻塞/非阻塞, 它们是程序在等待消息(无所谓同步或者异步)时的状态;

      2.同步/异步,是程序获得关注消息通知的机制。

      同步异步与阻塞,非阻塞组合

      1.同步阻塞

      效率最低(日志程序)。

      2.同步非阻塞

      效率也不高(需要轮询)。

      3.异步阻塞

      一般模式线程回调。

      4.异步非阻塞

      IOCP

      实例

      import redis

      import Queue

      import time

      from threading import Thread

      def handle_callback( res ) :

      print ' get redis res : ' , res

      class SetData :

      def __init__( self , key , value , handle ) :

      self.key = key

      self.value = value

      self.handle = handle

      class RedisAsyncHandle ( Thread ) :

      queue = Queue.Queue ( maxsize = 1024 )

      r = redis.Redis ( host = ' localhost ' , port = 6379 , db = 0 )

      def send_set_cmd ( self , key , value ) :

      set_data = SetData ( key , value , handle_callback )

      self.queue.put( set_data )

      def run ( self ) :

      while True :

      while not self.queue.empty():

      item = self.queue.get()

      print ' get item '

      res = self.r.set ( item.key , item.value )

      item.handle( res )

      time.sleep( 0.1 )

      handle = RedisAsyncHandle()

      handle.start()

      handle.send_set_cmd ( ' name1 ' , ' allen1 ' )

      handle.join()

      执行结果:

     

    原文链接:http://www.maiziedu.com/wiki/frame/together/

  • 相关阅读:
    BNU 51002 BQG's Complexity Analysis
    BNU OJ 51003 BQG's Confusing Sequence
    BNU OJ 51000 BQG's Random String
    BNU OJ 50999 BQG's Approaching Deadline
    BNU OJ 50998 BQG's Messy Code
    BNU OJ 50997 BQG's Programming Contest
    CodeForces 609D Gadgets for dollars and pounds
    CodeForces 609C Load Balancing
    CodeForces 609B The Best Gift
    CodeForces 609A USB Flash Drives
  • 原文地址:https://www.cnblogs.com/space007/p/6249608.html
Copyright © 2011-2022 走看看