本文整理汇总了Python中gevent.coros.Semaphore.release方法的典型用法代码示例。如果您正苦于以下问题:Python Semaphore.release方法的具体用法?Python Semaphore.release怎么用?Python Semaphore.release使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类gevent.coros.Semaphore
的用法示例。
在下文中一共展示了Semaphore.release方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: imap_manager
# 需要导入模块: from gevent.coros import Semaphore [as 别名]
# 或者: from gevent.coros.Semaphore import release [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))
示例2: GeventedHTTPTransport
# 需要导入模块: from gevent.coros import Semaphore [as 别名]
# 或者: from gevent.coros.Semaphore import release [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()
示例3: __init__
# 需要导入模块: from gevent.coros import Semaphore [as 别名]
# 或者: from gevent.coros.Semaphore import release [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
示例4: Client
# 需要导入模块: from gevent.coros import Semaphore [as 别名]
# 或者: from gevent.coros.Semaphore import release [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()
示例5: test
# 需要导入模块: from gevent.coros import Semaphore [as 别名]
# 或者: from gevent.coros.Semaphore import release [as 别名]
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
示例6: test_release_twice
# 需要导入模块: from gevent.coros import Semaphore [as 别名]
# 或者: from gevent.coros.Semaphore import release [as 别名]
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'])
示例7: handle_socket
# 需要导入模块: from gevent.coros import Semaphore [as 别名]
# 或者: from gevent.coros.Semaphore import release [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()
示例8: GeventSemaphore
# 需要导入模块: from gevent.coros import Semaphore [as 别名]
# 或者: from gevent.coros.Semaphore import release [as 别名]
class GeventSemaphore(BaseSemaphore):
def __init__(self):
self.__semaphore = Semaphore()
def acquire(self):
self.__semaphore.acquire()
def release(self):
self.__semaphore.release()
示例9: BaseSensor
# 需要导入模块: from gevent.coros import Semaphore [as 别名]
# 或者: from gevent.coros.Semaphore import release [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
示例10: __init__
# 需要导入模块: from gevent.coros import Semaphore [as 别名]
# 或者: from gevent.coros.Semaphore import release [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()
示例11: HitCounter
# 需要导入模块: from gevent.coros import Semaphore [as 别名]
# 或者: from gevent.coros.Semaphore import release [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()
示例12: BlockingDeque
# 需要导入模块: from gevent.coros import Semaphore [as 别名]
# 或者: from gevent.coros.Semaphore import release [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
示例13: SessionPool
# 需要导入模块: from gevent.coros import Semaphore [as 别名]
# 或者: from gevent.coros.Semaphore import release [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()
示例14: Pool
# 需要导入模块: from gevent.coros import Semaphore [as 别名]
# 或者: from gevent.coros.Semaphore import release [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 add(self, greenlet):
self._semaphore.acquire()
try:
Group.add(self, greenlet)
except:
self._semaphore.release()
raise
def _discard(self, greenlet):
Group._discard(self, greenlet)
self._semaphore.release()
示例15: Client
# 需要导入模块: from gevent.coros import Semaphore [as 别名]
# 或者: from gevent.coros.Semaphore import release [as 别名]
class Client(object):
px_per_tick = 10
def __init__(self, canvas, socket, address):
self.canvas = canvas
self.socket = socket
self.address = address
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)
print 'CONNECT', address
def send(self, line):
self.sendbuffer.append(line.strip() + '\n')
def disconnect(self):
print 'DISCONNECT', self.address
self.socket.close()
del self.canvas.clients[self.address]
def serve(self):
sendall = self.socket.sendall
readline = self.socket.makefile().readline
try:
while True:
# Idea: Sand first, recieve later. If the client is to
# slow to get the sendbuffer empty, he cannot send.
while self.sendbuffer:
sendall(self.sendbuffer.popleft())
line = readline()
if not line:
break
arguments = line.split()
command = arguments.pop(0)
if command == 'PX':
self.on_PX(arguments)
elif command == 'SIZE':
self.on_SIZE(arguments)
finally:
self.disconnect()
def on_SIZE(self, args):
self.send('SIZE %d %d' % self.canvas.get_size())
def on_PX(self, args):
self.limit.acquire()
x,y,color = args
x,y = int(x), int(y)
c = int(color, 16)
if c <= 16777215:
r = (c & 0xff0000) >> 16
g = (c & 0x00ff00) >> 8
b = c & 0x0000ff
a = 0xff
else:
r = (c & 0xff000000) >> 24
g = (c & 0x00ff0000) >> 16
b = (c & 0x0000ff00) >> 8
a = c & 0x000000ff
self.canvas.set_pixel(x, y, r, g, b, a)
def tick(self):
while self.limit.counter <= self.px_per_tick:
self.limit.release()