本文整理汇总了Python中eventlet.greenio.GreenSocket方法的典型用法代码示例。如果您正苦于以下问题:Python greenio.GreenSocket方法的具体用法?Python greenio.GreenSocket怎么用?Python greenio.GreenSocket使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类eventlet.greenio
的用法示例。
在下文中一共展示了greenio.GreenSocket方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: run
# 需要导入模块: from eventlet import greenio [as 别名]
# 或者: from eventlet.greenio import GreenSocket [as 别名]
def run(self):
acceptors = []
for sock in self.sockets:
gsock = GreenSocket(sock)
gsock.setblocking(1)
hfun = partial(self.handle, gsock)
acceptor = eventlet.spawn(_eventlet_serve, gsock, hfun,
self.worker_connections)
acceptors.append(acceptor)
eventlet.sleep(0.0)
while self.alive:
self.notify()
eventlet.sleep(1.0)
self.notify()
try:
with eventlet.Timeout(self.cfg.graceful_timeout) as t:
[a.kill(eventlet.StopServe()) for a in acceptors]
[a.wait() for a in acceptors]
except eventlet.Timeout as te:
if te != t:
raise
[a.kill() for a in acceptors]
示例2: _init_events_pipe
# 需要导入模块: from eventlet import greenio [as 别名]
# 或者: from eventlet.greenio import GreenSocket [as 别名]
def _init_events_pipe(self):
"""Create a self-pipe for the native thread to synchronize on.
This code is taken from the eventlet tpool module, under terms
of the Apache License v2.0.
"""
self._event_queue = native_Queue.Queue()
try:
rpipe, wpipe = os.pipe()
self._event_notify_send = greenio.GreenPipe(wpipe, 'wb', 0)
self._event_notify_recv = greenio.GreenPipe(rpipe, 'rb', 0)
except (ImportError, NotImplementedError):
# This is Windows compatibility -- use a socket instead
# of a pipe because pipes don't really exist on Windows.
sock = native_socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(('localhost', 0))
sock.listen(50)
csock = native_socket.socket(socket.AF_INET, socket.SOCK_STREAM)
csock.connect(('localhost', sock.getsockname()[1]))
nsock, addr = sock.accept()
self._event_notify_send = nsock.makefile('wb', 0)
gsock = greenio.GreenSocket(csock)
self._event_notify_recv = gsock.makefile('rb', 0)
示例3: run
# 需要导入模块: from eventlet import greenio [as 别名]
# 或者: from eventlet.greenio import GreenSocket [as 别名]
def run(self):
acceptors = []
for sock in self.sockets:
gsock = GreenSocket(sock)
gsock.setblocking(1)
hfun = partial(self.handle, gsock)
acceptor = eventlet.spawn(_eventlet_serve, gsock, hfun,
self.worker_connections)
acceptors.append(acceptor)
eventlet.sleep(0.0)
while self.alive:
self.notify()
try:
eventlet.sleep(1.0)
except AssertionError:
self.alive = False
break
self.notify()
try:
with eventlet.Timeout(self.cfg.graceful_timeout) as t:
[a.kill(eventlet.StopServe()) for a in acceptors]
[a.wait() for a in acceptors]
except eventlet.Timeout as te:
if te != t:
raise
[a.kill() for a in acceptors]
示例4: setup
# 需要导入模块: from eventlet import greenio [as 别名]
# 或者: from eventlet.greenio import GreenSocket [as 别名]
def setup():
global _rsock, _wsock, _coro, _setup_already, _rspq, _reqq
if _setup_already:
return
else:
_setup_already = True
assert _nthreads >= 0, "Can't specify negative number of threads"
if _nthreads == 0:
import warnings
warnings.warn("Zero threads in tpool. All tpool.execute calls will\
execute in main thread. Check the value of the environment \
variable EVENTLET_THREADPOOL_SIZE.", RuntimeWarning)
_reqq = Queue(maxsize=-1)
_rspq = Queue(maxsize=-1)
# connected socket pair
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(('127.0.0.1', 0))
sock.listen(1)
csock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
csock.connect(sock.getsockname())
csock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, True)
_wsock, _addr = sock.accept()
_wsock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, True)
sock.close()
_rsock = greenio.GreenSocket(csock)
for i in six.moves.range(_nthreads):
t = threading.Thread(target=tworker,
name="tpool_thread_%s" % i)
t.setDaemon(True)
t.start()
_threads.append(t)
_coro = greenthread.spawn_n(tpool_trampoline)
# This yield fixes subtle error with GreenSocket.__del__
eventlet.sleep(0)
# Avoid ResourceWarning unclosed socket on Python3.2+
示例5: fromfd
# 需要导入模块: from eventlet import greenio [as 别名]
# 或者: from eventlet.greenio import GreenSocket [as 别名]
def fromfd(*args):
return socket(__original_fromfd__(*args))
示例6: socketpair
# 需要导入模块: from eventlet import greenio [as 别名]
# 或者: from eventlet.greenio import GreenSocket [as 别名]
def socketpair(*args):
one, two = __original_socketpair__(*args)
return socket(one), socket(two)
示例7: test_connect_timeout
# 需要导入模块: from eventlet import greenio [as 别名]
# 或者: from eventlet.greenio import GreenSocket [as 别名]
def test_connect_timeout(self):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(0.1)
gs = greenio.GreenSocket(s)
try:
expect_socket_timeout(gs.connect, ('192.0.2.1', 80))
except socket.error as e:
# unreachable is also a valid outcome
if not get_errno(e) in (errno.EHOSTUNREACH, errno.ENETUNREACH):
raise
示例8: test_accept_timeout
# 需要导入模块: from eventlet import greenio [as 别名]
# 或者: from eventlet.greenio import GreenSocket [as 别名]
def test_accept_timeout(self):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('', 0))
s.listen(50)
s.settimeout(0.1)
gs = greenio.GreenSocket(s)
expect_socket_timeout(gs.accept)
示例9: test_connect_ex_timeout
# 需要导入模块: from eventlet import greenio [as 别名]
# 或者: from eventlet.greenio import GreenSocket [as 别名]
def test_connect_ex_timeout(self):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(0.1)
gs = greenio.GreenSocket(s)
e = gs.connect_ex(('192.0.2.1', 80))
if e not in (errno.EHOSTUNREACH, errno.ENETUNREACH):
self.assertEqual(e, errno.EAGAIN)
示例10: test_recv_timeout
# 需要导入模块: from eventlet import greenio [as 别名]
# 或者: from eventlet.greenio import GreenSocket [as 别名]
def test_recv_timeout(self):
listener = greenio.GreenSocket(socket.socket())
listener.bind(('', 0))
listener.listen(50)
evt = event.Event()
def server():
# accept the connection in another greenlet
sock, addr = listener.accept()
evt.wait()
gt = eventlet.spawn(server)
addr = listener.getsockname()
client = greenio.GreenSocket(socket.socket())
client.settimeout(0.1)
client.connect(addr)
expect_socket_timeout(client.recv, 0)
expect_socket_timeout(client.recv, 8192)
evt.send()
gt.wait()
示例11: test_recvfrom_timeout
# 需要导入模块: from eventlet import greenio [as 别名]
# 或者: from eventlet.greenio import GreenSocket [as 别名]
def test_recvfrom_timeout(self):
gs = greenio.GreenSocket(
socket.socket(socket.AF_INET, socket.SOCK_DGRAM))
gs.settimeout(.1)
gs.bind(('', 0))
expect_socket_timeout(gs.recvfrom, 0)
expect_socket_timeout(gs.recvfrom, 8192)
示例12: test_recv_into_timeout
# 需要导入模块: from eventlet import greenio [as 别名]
# 或者: from eventlet.greenio import GreenSocket [as 别名]
def test_recv_into_timeout(self):
buf = array.array('B')
listener = greenio.GreenSocket(socket.socket())
listener.bind(('', 0))
listener.listen(50)
evt = event.Event()
def server():
# accept the connection in another greenlet
sock, addr = listener.accept()
evt.wait()
gt = eventlet.spawn(server)
addr = listener.getsockname()
client = greenio.GreenSocket(socket.socket())
client.settimeout(0.1)
client.connect(addr)
expect_socket_timeout(client.recv_into, buf)
evt.send()
gt.wait()
示例13: test_send_timeout
# 需要导入模块: from eventlet import greenio [as 别名]
# 或者: from eventlet.greenio import GreenSocket [as 别名]
def test_send_timeout(self):
self.reset_timeout(2)
listener = bufsized(eventlet.listen(('', 0)))
evt = event.Event()
def server():
# accept the connection in another greenlet
sock, addr = listener.accept()
sock = bufsized(sock)
evt.wait()
gt = eventlet.spawn(server)
addr = listener.getsockname()
client = bufsized(greenio.GreenSocket(socket.socket()))
client.connect(addr)
client.settimeout(0.00001)
msg = b"A" * 100000 # large enough number to overwhelm most buffers
# want to exceed the size of the OS buffer so it'll block in a
# single send
def send():
for x in range(10):
client.send(msg)
expect_socket_timeout(send)
evt.send()
gt.wait()
示例14: test_sendall_timeout
# 需要导入模块: from eventlet import greenio [as 别名]
# 或者: from eventlet.greenio import GreenSocket [as 别名]
def test_sendall_timeout(self):
listener = greenio.GreenSocket(socket.socket())
listener.bind(('', 0))
listener.listen(50)
evt = event.Event()
def server():
# accept the connection in another greenlet
sock, addr = listener.accept()
evt.wait()
gt = eventlet.spawn(server)
addr = listener.getsockname()
client = greenio.GreenSocket(socket.socket())
client.settimeout(0.1)
client.connect(addr)
# want to exceed the size of the OS buffer so it'll block
msg = b"A" * (8 << 20)
expect_socket_timeout(client.sendall, msg)
evt.send()
gt.wait()
示例15: test_datagram_socket_operations_work
# 需要导入模块: from eventlet import greenio [as 别名]
# 或者: from eventlet.greenio import GreenSocket [as 别名]
def test_datagram_socket_operations_work(self):
receiver = greenio.GreenSocket(socket.AF_INET, socket.SOCK_DGRAM)
receiver.bind(('127.0.0.1', 0))
address = receiver.getsockname()
sender = greenio.GreenSocket(socket.AF_INET, socket.SOCK_DGRAM)
# Two ways sendto can be called
sender.sendto(b'first', address)
sender.sendto(b'second', 0, address)
sender_address = ('127.0.0.1', sender.getsockname()[1])
eq_(receiver.recvfrom(1024), (b'first', sender_address))
eq_(receiver.recvfrom(1024), (b'second', sender_address))