当前位置: 首页>>代码示例>>Python>>正文


Python Semaphore.acquire方法代码示例

本文整理汇总了Python中gevent.coros.Semaphore.acquire方法的典型用法代码示例。如果您正苦于以下问题:Python Semaphore.acquire方法的具体用法?Python Semaphore.acquire怎么用?Python Semaphore.acquire使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在gevent.coros.Semaphore的用法示例。


在下文中一共展示了Semaphore.acquire方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: imap_manager

# 需要导入模块: from gevent.coros import Semaphore [as 别名]
# 或者: from gevent.coros.Semaphore import acquire [as 别名]
class imap_manager(object):
    def __init__(self):
        from gevent.coros import Semaphore
        self.client = None
        self.sem = Semaphore(1)
        self.count = 0

    def close(self, current):
        import gevent
        gevent.sleep(360)
        self.sem.acquire()
        if self.client is not None and self.count == current:
            self.client.close()
            self.client = None
        self.sem.release()

    @contextmanager
    def get(self):
        import gevent
        self.count += 1
        self.sem.acquire()
        self.count += 1
        if self.client is None:
            from rbit import config
            from rbit import backend
            from rbit import imap
            cfg = config.Config('config', backend.create_session)
            self.client = imap.IMAPClient.from_config(cfg)
        yield self.client
        self.sem.release()
        gevent.spawn(lambda : self.close(self.count))
开发者ID:luispedro,项目名称:rbit,代码行数:33,代码来源:resources.py

示例2: GeventedHTTPTransport

# 需要导入模块: from gevent.coros import Semaphore [as 别名]
# 或者: from gevent.coros.Semaphore import acquire [as 别名]
class GeventedHTTPTransport(HTTPTransport):

    scheme = ['gevent+http', 'gevent+https']

    def __init__(self, parsed_url, maximum_outstanding_requests=100):
        if not gevented:
            raise ImportError('GeventedHTTPTransport requires gevent.')
        self._lock = Semaphore(maximum_outstanding_requests)

        super(GeventedHTTPTransport, self).__init__(parsed_url)

        # remove the gevent+ from the protocol, as it is not a real protocol
        self._url = self._url.split('+', 1)[-1]

    def send(self, data, headers):
        """
        Spawn an async request to a remote webserver.
        """
        # this can be optimized by making a custom self.send that does not
        # read the response since we don't use it.
        self._lock.acquire()
        return spawn(super(GeventedHTTPTransport, self).send, data, headers).link(self._done, self)

    def _done(self, *args):
        self._lock.release()
开发者ID:openlabs,项目名称:raven,代码行数:27,代码来源:transport.py

示例3: __init__

# 需要导入模块: from gevent.coros import Semaphore [as 别名]
# 或者: from gevent.coros.Semaphore import acquire [as 别名]
class MediaCodec_Base:	
	def __init__(self,svc):
		#maxsize - 最大缓存,超过限制将视为无效解码
		self.buf =''
		self.conn = None
		self.svc = svc
		self.mtx = Semaphore()
		self.msgcnt=0 #接收消息计数

	def parseMessage(self,s):
		pass


	#数据置入队列,应检查队列长度和数据合法性
	def queueIn(self,s,conn):
		self.mtx.acquire()
#		self.buflist.append(s)
		self.buf+=s
		self.mtx.release()
		return True


	
	def decode(self):
		#@return (packets,retry) retry表示解码遇到错误或者数据非法
		return (),False


	#根据传入的消息分拣出gps,alarm消息  MsgAoModule_Alarm(), MsgAoModule_GpsData()
	def filter_msg(self,m,aom):
		# GSP,ALARM 消息是关心的消息,需要反馈到客户端
		return (m,)
	
	def command(self,aom,m):
		pass

	def save(self,aom,m):
		'''
			保存设备所有信息,不区分是何类型
		'''
		try:
			cm = aom.ao.cm
			params= m.params
			if isinstance(m.params,dict):
				params = json.dumps(m.params)
			log = cm.AO_ModuleLog()
			log.ao = aom.ao.r
			log.module = aom.r
			log.type = ModuleMsgType.DEV2SYS
			log.time = datetime.datetime.now()
			log.msgtype = 'N/A'
			log.params = params
			log.rawmsg = m.rawmsg
			log.seq = 0
			log.save()
			return True
		except:
			traceback.print_exc()
			return False
开发者ID:adoggie,项目名称:NewGis2013,代码行数:61,代码来源:codec.py

示例4: Client

# 需要导入模块: from gevent.coros import Semaphore [as 别名]
# 或者: from gevent.coros.Semaphore import acquire [as 别名]
class Client(object):
    px_per_tick = 100
    
    def __init__(self, canvas):
        self.canvas = canvas
        self.socket = None
        self.connect_ts = time.time()
        # This buffer discards all but the newest 1024 messages
        self.sendbuffer = deque([], 1024)
        # And this is used to limit clients to X messages per tick
        # We start at 0 (instead of x) to add a reconnect-penalty.
        self.limit = Semaphore(0)
        self.lock = RLock()

    def send(self, line):
        self.sendbuffer.append(line.strip() + '\n')

    def nospam(self, line):
        if not self.sendbuffer:
            self.sendbuffer.append(line.strip() + '\n')

    def disconnect(self):
        with self.lock:
            if self.socket:
                socket = self.socket
                self.socket = None
                socket.close()
                log.info('Disconnect')

    def serve(self, socket):
        with self.lock:
            self.socket = socket
            sendall = self.socket.sendall
            readline = self.socket.makefile().readline

        try:
            while self.socket:
                self.limit.acquire()
                # Idea: Send first, receive later. If the client is to
                # slow to get the send-buffer empty, he cannot send.
                while self.sendbuffer:
                    sendall(self.sendbuffer.popleft())
                line = readline().strip()
                if not line:
                    break
                arguments = line.split()
                command = arguments.pop(0)
                try:
                    self.canvas.fire('COMMAND-%s' % command.upper(), self, *arguments)
                except Exception, e:
                    socket.send('ERROR %r :(' % e)
                    break
        finally:
            self.disconnect()

    def tick(self):
        while self.limit.counter <= self.px_per_tick:
            self.limit.release()
开发者ID:Falk2704,项目名称:pixelflut,代码行数:60,代码来源:pixelflut.py

示例5: handle_socket

# 需要导入模块: from gevent.coros import Semaphore [as 别名]
# 或者: from gevent.coros.Semaphore import acquire [as 别名]
def handle_socket(sock, address):
    semaphore = Semaphore()
    while 1:
        sock.setblocking(0)
        semaphore.acquire()
        sockfd = sock.makefile()
        chunk_size = 1024
        head_packet_format = "!LL128s128sL"
        head_packet_size = struct.calcsize(head_packet_format)
        data = sockfd.read(head_packet_size)
        if not data or len(data) != head_packet_size:
            return
        filepath_len, filename_len, filepath,filename, filesize = struct.unpack(head_packet_format,data)
        filepath = filepath[:filepath_len]
        filename = filename[:filename_len]
        #logger.debug("update file: %s" % filepath + '/' + filename)
        
        fd = open(filename,'wb')
        fcntl.flock(fd,fcntl.LOCK_EX)
        print "File %s size: %s" % (filename, filesize)
        print 111111111111111
        writen_size = 0
        if filesize > chunk_size:
            times = filesize / chunk_size
            first_part_size = times * chunk_size
            second_part_size = filesize % chunk_size
            print "times: %s  first_part_size:%s  second_part_size:%s" % (times,first_part_size,second_part_size)
            print 22222222222222222222
            #receive first part packets
            while 1:
                data = sockfd.read(chunk_size)
                fd.write(data)
                fd.flush()
                writen_size += len(data)
                if writen_size == first_part_size:
                    break
            print "writen_size in first_par: %s" % writen_size
            print 333333333333333333333
            if second_part_size:
                #receive the packet at last
                data = sockfd.read(second_part_size)
                fd.write(data)
                fd.flush()
                writen_size += len(data)
            print 4444444444444444444444
        else:
            data = sockfd.read(filesize)
            fd.write(data)
            fd.flush()
            writen_size += len(data)
            
        fcntl.flock(fd,fcntl.LOCK_UN)
        fd.close()
        print '555555555555555555555'
        print "File %s size: %s\n" % (filename, writen_size)
        semaphore.release()
开发者ID:darrentangdt,项目名称:cloud_manage_server,代码行数:58,代码来源:syncserver_gevent.py

示例6: GeventSemaphore

# 需要导入模块: from gevent.coros import Semaphore [as 别名]
# 或者: from gevent.coros.Semaphore import acquire [as 别名]
class GeventSemaphore(BaseSemaphore):

    def __init__(self):
        self.__semaphore = Semaphore()

    def acquire(self):
        self.__semaphore.acquire()

    def release(self):
        self.__semaphore.release()
开发者ID:connoryang,项目名称:dec-eve-serenity,代码行数:12,代码来源:geventimpl.py

示例7: BaseSensor

# 需要导入模块: from gevent.coros import Semaphore [as 别名]
# 或者: from gevent.coros.Semaphore import acquire [as 别名]
class BaseSensor(Greenlet, Model):
    """
    Base class for sensors.  
    Each sensor is associated with a Docker container by its 
    cid (or name, if you prefer).  
    A sensor has to be started with its `start` method, eventually, 
    stopped with its `kill` method.

    Every sensor provides collected data by means of 
    `get_data` method which returns a generator.
    Each call to `next` value is blocking for a time declared 
    on `__init__` of the sensor through `spacing` parameter.
    """
    cid = StringType(required=True)
    spacing = FloatType(default=0.1)

    @serializable
    def uid(self):
        return get_uid(self.__class__, self.cid)

    def __init__(self, *args, **kwargs):
        Greenlet.__init__(self)
        Model.__init__(self, *args, **kwargs)

        docker_url = config.get('docker_url')
        self.container = api.DockerAPI(self.cid, docker_url)

        self._lock = Semaphore()
        self._lock.acquire() # locking semaphore
        self._new_data = None

    @raise_connection_error
    def _run(self):
        while True:
            self._new_data = self._get()
            LOG.debug("{} got {}".format(self.uid, self._new_data))
            self._store(self._new_data)
            self._lock.release()
            sleep(self.spacing)

    def _get(self):
        """Override"""
        return None

    def _store(self, data):
        """Override"""
        pass

    def get_data(self):
        while True:
            if not self.started or self.dead:
                raise SensorError("Start the sensor before getting data.")
            self._lock.acquire()
            yield self._new_data
开发者ID:deib-polimi,项目名称:sy,代码行数:56,代码来源:base.py

示例8: __init__

# 需要导入模块: from gevent.coros import Semaphore [as 别名]
# 或者: from gevent.coros.Semaphore import acquire [as 别名]
    class Sema:
        def __init__(self):
            self.sem = Semaphore()
            self.timestamp = datetime.datetime.now()
            self.count = 1
            self.sem.acquire(blocking=True)

        def join(self):
            self.count += 1
            self.sem.acquire(blocking=True)

        def release(self):
            for ii in xrange(self.count):
                self.sem.release()
开发者ID:mizjogit,项目名称:sakemon,代码行数:16,代码来源:serva.py

示例9: websocket

# 需要导入模块: from gevent.coros import Semaphore [as 别名]
# 或者: from gevent.coros.Semaphore import acquire [as 别名]
def websocket(token):
    if request.environ.get('wsgi.websocket'):
        email = database_helper.get_active(token)
        if (email == None) :
#            return json.dumps({"success": False, "message": "Your are not signed in."})
        else :
            websocket = request.environ['wsgi.websocket']
            sema = Semaphore(0)
            websockets[email] = {"websocket": websocket, "sema": sema}
            print "websocket(): waiting at sema for [" + email + "]"
            sema.acquire()
            print "websocket(): sema for [" + email + "] passed"
#            return json.dumps({"success": True, "message": "Websocket connected."})
    return "websocket(): done"
开发者ID:postinternetsyndrome,项目名称:tddd97,代码行数:16,代码来源:server.py

示例10: HitCounter

# 需要导入模块: from gevent.coros import Semaphore [as 别名]
# 或者: from gevent.coros.Semaphore import acquire [as 别名]
class HitCounter(object):
    
    def __init__(self):
        self.value = 0
        self.lock = Semaphore(1)
    
    def __str__(self):
        return str(self.value)

    def increment(self):
        try:
            self.lock.acquire()
            self.value += 1
        finally:
            self.lock.release()
开发者ID:numberoverzero,项目名称:cat_facts,代码行数:17,代码来源:__init__.py

示例11: Pool

# 需要导入模块: from gevent.coros import Semaphore [as 别名]
# 或者: from gevent.coros.Semaphore import acquire [as 别名]
class Pool(Group):

    def __init__(self, size=None, greenlet_class=None):
        if size is not None and size < 0:
            raise ValueError('size must not be negative: %r' % (size, ))
        Group.__init__(self)
        self.size = size
        if greenlet_class is not None:
            self.greenlet_class = greenlet_class
        if size is None:
            self._semaphore = DummySemaphore()
        else:
            self._semaphore = Semaphore(size)

    def wait_available(self):
        self._semaphore.wait()

    def full(self):
        return self.free_count() <= 0

    def free_count(self):
        if self.size is None:
            return 1
        return max(0, self.size - len(self))

    def start(self, greenlet):
        self._semaphore.acquire()
        try:
            self.add(greenlet)
        except:
            self._semaphore.release()
            raise
        greenlet.start()

    def spawn(self, *args, **kwargs):
        self._semaphore.acquire()
        try:
            greenlet = self.greenlet_class.spawn(*args, **kwargs)
            self.add(greenlet)
        except:
            self._semaphore.release()
            raise
        return greenlet

    def discard(self, greenlet):
        Group.discard(self, greenlet)
        self._semaphore.release()
开发者ID:christopherhesse,项目名称:gevent-pypy-experimental,代码行数:49,代码来源:pool.py

示例12: BlockingDeque

# 需要导入模块: from gevent.coros import Semaphore [as 别名]
# 或者: from gevent.coros.Semaphore import acquire [as 别名]
class BlockingDeque(deque):

    def __init__(self, *args, **kwargs):
        super(BlockingDeque, self).__init__(*args, **kwargs)
        self.sema = Semaphore(len(self))

    def append(self, *args, **kwargs):
        ret = super(BlockingDeque, self).append(*args, **kwargs)
        self.sema.release()
        return ret

    def appendleft(self, *args, **kwargs):
        ret = super(BlockingDeque, self).appendleft(*args, **kwargs)
        self.sema.release()
        return ret

    def clear(self, *args, **kwargs):
        ret = super(BlockingDeque, self).clear(*args, **kwargs)
        while not self.sema.locked():
            self.sema.acquire(blocking=False)
        return ret

    def extend(self, *args, **kwargs):
        pre_n = len(self)
        ret = super(BlockingDeque, self).extend(*args, **kwargs)
        post_n = len(self)
        for i in xrange(pre_n, post_n):
            self.sema.release()
        return ret

    def extendleft(self, *args, **kwargs):
        pre_n = len(self)
        ret = super(BlockingDeque, self).extendleft(*args, **kwargs)
        post_n = len(self)
        for i in xrange(pre_n, post_n):
            self.sema.release()
        return ret

    def pop(self, *args, **kwargs):
        self.sema.acquire()
        return super(BlockingDeque, self).pop(*args, **kwargs)

    def popleft(self, *args, **kwargs):
        self.sema.acquire()
        return super(BlockingDeque, self).popleft(*args, **kwargs)

    def remove(self, *args, **kwargs):
        ret = super(BlockingDeque, self).remove(*args, **kwargs)
        self.sema.acquire()
        return ret
开发者ID:rafaelnovello,项目名称:python-slimta,代码行数:52,代码来源:deque.py

示例13: SessionPool

# 需要导入模块: from gevent.coros import Semaphore [as 别名]
# 或者: from gevent.coros.Semaphore import acquire [as 别名]
class SessionPool(object):
    """
    This class is intended to allow for the model to be used in a threaded
    environment.
    """

    def __init__(self, engine, min_sessions=10, max_sessions=25):
        self.engine = engine
        sm = sessionmaker(autoflush=False, autocommit=False, bind=engine)
        self.session = scoped_session(sm)

        self.min_sessions = min_sessions
        self.max_sessions = max_sessions

        self.session_pool = []
        self.available = []
        self.checkouts = {}
        self.sessions = local()
        self.lock = Semaphore()

    def checkin(self):
        self.lock.acquire()
        try:
            session = self.sessions.session
            if session:
                session.close()
        finally:
            self.lock.release()

    def checkout(self):
        self.lock.acquire()
        try:
            session = self.sessions.session = self.session()
            return session
        finally:
            self.lock.release()
开发者ID:duongthienduc,项目名称:vmail,代码行数:38,代码来源:__init__.py

示例14: RateLimiterState

# 需要导入模块: from gevent.coros import Semaphore [as 别名]
# 或者: from gevent.coros.Semaphore import acquire [as 别名]
class RateLimiterState(object):
    def __init__(self, fail_limit, fail_period, blackout_wait):
        self.__local_rlservice = None
        self.__request_fails = 0
        self.__fails = deque()
        self.__fail_limit = fail_limit
        self.__fail_period = fail_period
        self.__blackout_start = None
        self.__blackout_wait = blackout_wait
        self.__is_ec2 = utils.is_ec2()
        self.__service_init_semaphore = Semaphore()
        stack_info = libs.ec2_utils.get_stack()
        self.__stack_name = 'localhost'
        self.__node_name = 'localhost'
        if stack_info is not None:
            self.__stack_name = stack_info.instance.stack
            self.__node_name = stack_info.instance.name
        self.__last_email_time = 0
        self.__emails = deque()

        # determine the private ip address of the ratelimiter instance for this stack
        self._getHost()
        print('### host: %s' % self.__host)

    class FailLog(object):
        def __init__(self, exception):
            self.timestamp = time.time()
            self.exception = exception

    @property
    def _local_rlservice(self):
        if self.__local_rlservice is None:
            # use a semaphore here because if two requests come in immediately, we might instantiate two services
            self.__service_init_semaphore.acquire()
            if self.__local_rlservice is None:
                if self.__is_ec2:
                    self.__local_rlservice = StampedRateLimiterService(throttle=True)
                else:
                    self.__local_rlservice = StampedRateLimiterService(throttle=False)
            self.__service_init_semaphore.release()

        return self.__local_rlservice

    def _getHost(self):
        ratelimiter_nodes = None
        try:
            ratelimiter_nodes = libs.ec2_utils.get_nodes('ratelimiter')
        except:
            logs.warning("Could not find a node with tag 'ratelimiter' on same stack")
        if ratelimiter_nodes is None:
            self.__host = 'localhost'
        else:
            self.__host = ratelimiter_nodes[0]['private_ip_address']
        self.__port = 18861

    def sendFailLogEmail(self):
        if len(self.__fails) == 0:
            return

        output = '<html>'
        output += "<h3>RateLimiter RPC Server Failure on %s</h3>" % self.__stack_name
        output += "<p>On stack '%s' instance '%s'.</p>" % (self.__stack_name, self.__node_name)
        output += "<p><i>There were %s failed requests to the rpc server within the last %s seconds</i></p>" %\
                  (self.__fail_limit, self.__fail_period)
        back_online = time.strftime('%m/%d/%Y %H:%M:%S', time.localtime(self.__blackout_start + self.__blackout_wait)) # Timestamp
        output += "<p>Waiting for %s seconds.  Will use local Rate Limiter service until: %s</p>" % (self.__blackout_wait, back_online)

        output += '<h3>Fail Log</h3>'

        output += '<table border=1 cellpadding=5>'
        output += '<tr>'
        labels = ['Timestamp', 'Exception']
        for label in labels:
            output += '<td style="font-weight:bold">%s</td>' % label
        output += '</tr>'

        for fail in self.__fails:
            output += '<tr>'
            output += '<td valign=top>%s</td>' % time.strftime('%m/%d/%Y %H:%M:%S', time.localtime(fail.timestamp)) # Timestamp
            output += '<td valign=top>%s</td>' % fail.exception
            output += '</tr>'

        output += '</table>'

        output += '</html>'


        try:
            email = {}
            email['from'] = 'Stamped <[email protected]>'
            email['to'] = '[email protected]'
            email['subject'] = "%s RateLimiter RPC server failure" % self.__stack_name
            email['body'] = output
            utils.sendEmail(email, format='html')
        except Exception as e:
            print('UNABLE TO SEND EMAIL: %s' % e)

        return output

    def _fail(self, exception):
#.........这里部分代码省略.........
开发者ID:Stamped,项目名称:Stamped,代码行数:103,代码来源:Request.py

示例15: Semaphore

# 需要导入模块: from gevent.coros import Semaphore [as 别名]
# 或者: from gevent.coros.Semaphore import acquire [as 别名]
from locust import HttpLocust, TaskSet, task, events

from gevent.coros import Semaphore
all_locusts_spawned = Semaphore()
all_locusts_spawned.acquire()

def on_hatch_complete(**kw):
    all_locusts_spawned.release()

events.hatch_complete += on_hatch_complete

class UserTasks(TaskSet):
    def on_start(self):
        all_locusts_spawned.wait()
        self.wait()
    
    @task
    def index(self):
        self.client.get("/")
    
class WebsiteUser(HttpLocust):
    host = "http://127.0.0.1:8089"
    min_wait = 2000
    max_wait = 5000
    task_set = UserTasks
开发者ID:DragonFlyBSD,项目名称:DPorts,代码行数:27,代码来源:semaphore_wait.py


注:本文中的gevent.coros.Semaphore.acquire方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。