本文整理汇总了Python中gevent.coros.Semaphore类的典型用法代码示例。如果您正苦于以下问题:Python Semaphore类的具体用法?Python Semaphore怎么用?Python Semaphore使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Semaphore类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: GeventedHTTPTransport
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()
示例2: __init__
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
示例3: Client
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()
示例4: test
def test():
s = Semaphore(0)
future1 = yield batchy_gevent.greenlet_future(gevent.spawn(acq, s))
future2 = yield batchy_gevent.greenlet_future(gevent.spawn(acq, s))
s.release()
yield future1
s.release()
yield future2
示例5: handle_socket
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()
示例6: GeventSemaphore
class GeventSemaphore(BaseSemaphore):
def __init__(self):
self.__semaphore = Semaphore()
def acquire(self):
self.__semaphore.acquire()
def release(self):
self.__semaphore.release()
示例7: __init__
def __init__(self, size=None, greenlet_class=None):
if size is not None and size < 0:
raise ValueError('Invalid size for pool (positive integer or None required): %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)
示例8: test_release_twice
def test_release_twice(self):
s = Semaphore()
result = []
s.rawlink(lambda s: result.append('a'))
s.release()
s.rawlink(lambda s: result.append('b'))
s.release()
gevent.sleep(0.001)
self.assertEqual(result, ['a', 'b'])
示例9: imap_manager
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))
示例10: websocket
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"
示例11: HitCounter
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()
示例12: __init__
def __init__(self, oc_gateway):
self._oc_gateway = oc_gateway
self._model_settings_update_in_progress = Semaphore()
self._vol_curves = {}
self._parent_symbol_to_sandbox_symbol_dict = {}
self._parent_month_id_to_sandbox_month_id_dict = {}
self._vol_curve_factory = VolCurveFactory(oc_gateway)
示例13: _reset
def _reset(self):
'''
reset all the variables used for keeping track of internal state
'''
#an list of Character()s
self.results = []
#an list of strings
self.str_results = []
#character generators take care of building the Character objects. we need one per row
self.char_gens = []
#a queue for communications between Character()s and request_makers
self.q = Queue()
#"threads" that run the Character()s
self.character_pool = Pool(self.concurrency)
#"threads" that make requests
self.request_makers = [gevent.spawn(self._request_maker) for i in range(self.concurrency)]
#fire this event when shutting down
self.shutting_down = Event()
#do we need to add more rows?
self.need_more_rows = True
#use this as a lock to know when not to mess with self.results
self.results_lock = Semaphore(1)
#request_count is the number of requests made on the current run
self.request_count = 0
#failure_count is the number of requests made on the current run
self.failure_count = 0
示例14: __new__
def __new__(cls,s):
if s.id in sites:
return sites[s.id]
self = object.__new__(cls)
sites[s.id] = self
self.s = s
self.connect()
self._delay_on = Semaphore()
self.controllers = set()
self.envgroups = set()
self.meters = {}
for M in METERS:
ml = set()
self.meters[M.meter_type] = ml
for d in getattr(self.s,M.meter_type+"_meters").all():
ml.add(M(d))
self.log("Startup")
self.connect_monitors(do_controllers=False)
signal.signal(signal.SIGINT,self.do_shutdown)
signal.signal(signal.SIGTERM,self.do_shutdown)
signal.signal(signal.SIGHUP,self.do_syncsched)
self.running = True
return self
示例15: __init__
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]