本文整理汇总了Python中contextlib.ExitStack.__exit__方法的典型用法代码示例。如果您正苦于以下问题:Python ExitStack.__exit__方法的具体用法?Python ExitStack.__exit__怎么用?Python ExitStack.__exit__使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类contextlib.ExitStack
的用法示例。
在下文中一共展示了ExitStack.__exit__方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Server
# 需要导入模块: from contextlib import ExitStack [as 别名]
# 或者: from contextlib.ExitStack import __exit__ [as 别名]
class Server(object):
def __init__(self, port):
self.context = None
self.thread = None
self.queue = None
self.webserver = None
self.port = port
def __enter__(self):
self.context = ExitStack()
self.context.enter_context(self.event_loop_context())
self.thread = EventLoopThread([self.webserver.server()])
self.context.enter_context(self.thread)
return self
def __exit__(self, *enc):
self.context.__exit__(*enc)
def recv(self):
result = self.queue.get()
return result
def send(self, msg):
asyncio.run_coroutine_threadsafe(self.webserver.broadcast(msg), self.thread.loop)
@contextmanager
def event_loop_context(self):
with ExitStack() as stack:
stack.callback(lambda: setattr(self, "queue", None))
stack.callback(lambda: setattr(self, "webserver", None))
self.queue = Queue()
self.webserver = WebsocketServer(self.queue, self.port)
yield
示例2: EventLoopThread
# 需要导入模块: from contextlib import ExitStack [as 别名]
# 或者: from contextlib.ExitStack import __exit__ [as 别名]
class EventLoopThread(object):
def __init__(self, servers_to_start):
self.context = None
self.executor = None
self.loop = None
self.servers_to_start = servers_to_start
self.servers = []
def __enter__(self):
self.context = ExitStack()
self.executor = self.context.enter_context(ThreadPoolExecutor(max_workers=1))
self.context.enter_context(self.event_loop_context())
return self
def __exit__(self, *enc):
self.context.__exit__(*enc)
self.context = None
self.executor = None
self.loop = None
def start_loop(self, event):
logger.info("starting eventloop server")
loop = asyncio.new_event_loop()
self.loop = loop
asyncio.set_event_loop(loop)
for server_starter in self.servers_to_start:
server = loop.run_until_complete(server_starter)
self.servers.append(server)
loop.call_soon(event.set)
loop.run_forever()
def stop_loop(self):
logger.info("stopping eventloop server")
self.loop.create_task(self._close_connections())
@contextmanager
def event_loop_context(self):
event = Event()
event.clear()
self.executor.submit(self.start_loop, event)
event.wait()
logger.info("started eventloop")
try:
yield
finally:
self.loop.call_soon_threadsafe(self.stop_loop)
logger.info("stopped eventloop")
@asyncio.coroutine
def _close_connections(self):
for server in self.servers:
server.close()
yield from server.wait_closed()
self.loop.stop()
示例3: WPRobotBase
# 需要导入模块: from contextlib import ExitStack [as 别名]
# 或者: from contextlib.ExitStack import __exit__ [as 别名]
class WPRobotBase(object):
def __init__(self):
self.context = None
self.devices = []
def __enter__(self):
wiringpi2.wiringPiSetupGpio()
self.context = ExitStack()
for device in self.devices:
self.context.enter_context(device)
return self
def __exit__(self, *exc):
self.context.__exit__(*exc)
def attach_device(self, device):
self.devices.append(device)
return device
示例4: RobotBase
# 需要导入模块: from contextlib import ExitStack [as 别名]
# 或者: from contextlib.ExitStack import __exit__ [as 别名]
class RobotBase(object):
def __init__(self):
self.context = None
self.devices = []
def __enter__(self):
GPIO.setmode(GPIO.BCM)
self.context = ExitStack()
for device in self.devices:
self.context.enter_context(device)
return self
def __exit__(self, *exc):
self.context.__exit__(*exc)
GPIO.cleanup()
def attach_device(self, device):
self.devices.append(device)
return device
示例5: __init__
# 需要导入模块: from contextlib import ExitStack [as 别名]
# 或者: from contextlib.ExitStack import __exit__ [as 别名]
class SentiData:
def __init__(self):
self._stack = ExitStack()
self.distant_docs = []
self.distant_labels = []
self.unsup_docs = []
def __enter__(self):
return self
def __exit__(self, *exc_details):
return self._stack.__exit__(*exc_details)
示例6: _exit
# 需要导入模块: from contextlib import ExitStack [as 别名]
# 或者: from contextlib.ExitStack import __exit__ [as 别名]
def _exit(self, exc_type, exc_val, exc_tb, daemon=False):
stack = ExitStack()
# called last
@stack.push
def exit_loop(exc_type, exc_val, exc_tb):
if self.client.is_closed:
return self._loop.__exit__(exc_type, exc_val, exc_tb)
if threading.current_thread() is threading.main_thread():
# TODO the main thread is not necessarily the last thread to finish.
# Should the signal handler be removed in case it isn't?
stack.push(self._signal_ctx)
# called first
# exit the client with the given daemon-ness, maybe leading the client to close
@stack.push
def exit_client(exc_type, exc_val, exc_tb):
return self._call(self.client._aexit(exc_type, exc_val, exc_tb, daemon=daemon))
return stack.__exit__(exc_type, exc_val, exc_tb)
示例7: _TestUser
# 需要导入模块: from contextlib import ExitStack [as 别名]
# 或者: from contextlib.ExitStack import __exit__ [as 别名]
class _TestUser(object):
def __init__(self, test_client, runestone_db_tools, username, password, course_name,
# True if the course is free (no payment required); False otherwise.
is_free=True):
self.test_client = test_client
self.runestone_db_tools = runestone_db_tools
self.username = username
self.first_name = 'test'
self.last_name = 'user'
self.email = self.username + '@foo.com'
self.password = password
self.course_name = course_name
self.is_free = is_free
def __enter__(self):
# Registration doesn't work unless we're logged out.
self.test_client.logout()
# Now, post the registration.
self.test_client.validate('default/user/register',
'Support Runestone Interactive' if self.is_free else 'Payment Amount',
data=dict(
username=self.username,
first_name=self.first_name,
last_name=self.last_name,
# The e-mail address must be unique.
email=self.email,
password=self.password,
password_two=self.password,
# Note that ``course_id`` is (on the form) actually a course name.
course_id=self.course_name,
accept_tcp='on',
donate='0',
_next='/runestone/default/index',
_formname='register',
)
)
# Schedule this user for deletion.
self.exit_stack_object = ExitStack()
self.exit_stack = self.exit_stack_object.__enter__()
self.exit_stack.callback(self._delete_user)
# Record IDs
db = self.runestone_db_tools.db
self.course_id = db(db.courses.course_name == self.course_name).select(db.courses.id).first().id
self.user_id = db(db.auth_user.username == self.username).select(db.auth_user.id).first().id
return self
# Clean up on exit by invoking all ``__exit__`` methods.
def __exit__(self, exc_type, exc_value, traceback):
self.exit_stack_object.__exit__(exc_type, exc_value, traceback)
# Delete the user created by entering this context manager. TODO: This doesn't delete all the chapter progress tracking stuff.
def _delete_user(self):
db = self.runestone_db_tools.db
# Delete the course this user registered for.
db(( db.user_courses.course_id == self.course_id) & (db.user_courses.user_id == self.user_id) ).delete()
# Delete the user.
db(db.auth_user.username == self.username).delete()
db.commit()
def login(self):
self.test_client.post('default/user/login', data=dict(
username=self.username,
password=self.password,
_formname='login',
))
def make_instructor(self, course_id=None):
# If ``course_id`` isn't specified, use this user's ``course_id``.
course_id = course_id or self.course_id
return self.runestone_db_tools.make_instructor(self.user_id, course_id)
# A context manager to update this user's profile. If a course was added, it returns that course's ID; otherwise, it returns None.
@contextmanager
def update_profile(self,
# This parameter is passed to ``test_client.validate``.
expected_string=None,
# An updated username, or ``None`` to use ``self.username``.
username=None,
# An updated first name, or ``None`` to use ``self.first_name``.
first_name=None,
# An updated last name, or ``None`` to use ``self.last_name``.
last_name=None,
# An updated email, or ``None`` to use ``self.email``.
email=None,
# An updated last name, or ``None`` to use ``self.course_name``.
course_name=None,
section='',
# A shortcut for specifying the ``expected_string``, which only applies if ``expected_string`` is not set. Use ``None`` if a course will not be added, ``True`` if the added course is free, or ``False`` if the added course is paid.
is_free=None,
# The value of the ``accept_tcp`` checkbox; provide an empty string to leave unchecked. The default value leaves it checked.
accept_tcp='on'):
if expected_string is None:
if is_free is None:
expected_string = 'Course Selection'
else:
#.........这里部分代码省略.........