Best Python code snippet using autotest_python
_background_server.py
Source: _background_server.py
1# Copyright 2018 Google LLC2#3# Licensed under the Apache License, Version 2.0 (the "License");4# you may not use this file except in compliance with the License.5# You may obtain a copy of the License at6#7# http://www.apache.org/licenses/LICENSE-2.08#9# Unless required by applicable law or agreed to in writing, software10# distributed under the License is distributed on an "AS IS" BASIS,11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12# See the License for the specific language governing permissions and13# limitations under the License.14"""Tornado server running in a background thread."""15from __future__ import absolute_import16from __future__ import division17from __future__ import print_function18import threading19import portpicker20import tornado21import tornado.httpserver22import tornado.ioloop23import tornado.web24class _BackgroundServer(object):25 """HTTP server that runs in a background thread."""26 def __init__(self, app):27 """Initialize (but do not start) background server.28 Args:29 app: server application to run.30 """31 self._app = app32 # These will be initialized when starting the server.33 self._port = None34 self._server_thread = None35 self._ioloop = None36 self._server = None37 @property38 def port(self):39 """Returns the current port or error if the server is not started.40 Raises:41 RuntimeError: If server has not been started yet.42 Returns:43 The port being used by the server.44 """45 if self._server_thread is None:46 raise RuntimeError('Server not running.')47 return self._port48 def stop(self):49 """Stops the server thread.50 Raises:51 RuntimeError: if server is already stopped.52 """53 if self._server_thread is None:54 raise RuntimeError('stop() called on stopped server')55 def shutdown():56 self._server.stop()57 self._ioloop.stop()58 try:59 self._ioloop.add_callback(shutdown)60 self._server_thread.join()61 self._ioloop.close(all_fds=True)62 finally:63 self._server_thread = None64 def start(self, port=None, timeout=1):65 """Starts a server in a thread using the WSGI application provided.66 Will wait until the thread has started calling with an already serving67 application will simple return.68 Args:69 port: Number of the port to use for the application, will find an open70 port if a nonzero port is not provided.71 timeout: Http timeout in seconds. Note that this is only respected under72 tornado v4.73 Raises:74 RuntimeError: if server is already started.75 """76 if self._server_thread is not None:77 raise RuntimeError('start() called on running background server.')78 self._port = port or portpicker.pick_unused_port()79 # Support both internal & external colab (tornado v3 vs. v4)80 # TODO(b/35548011): remove tornado v3 handling81 if tornado.version[0] >= '4':82 kwds = {'idle_connection_timeout': timeout, 'body_timeout': timeout}83 else:84 kwds = {}85 self._server = tornado.httpserver.HTTPServer(self._app, **kwds)86 self._ioloop = tornado.ioloop.IOLoop()87 def start_server(httpd, ioloop, port):88 # TODO(b/147233568): Restrict this to local connections.89 host = '' # Bind to all90 ioloop.make_current()91 httpd.listen(port=port, address=host)92 ioloop.start()93 self._server_thread = threading.Thread(94 target=start_server,95 kwargs={96 'httpd': self._server,97 'ioloop': self._ioloop,98 'port': self._port99 })100 started = threading.Event()101 self._ioloop.add_callback(started.set)102 self._server_thread.start()...
Check out the latest blogs from LambdaTest on this topic:
Before we discuss Scala testing, let us understand the fundamentals of Scala and how this programming language is a preferred choice for your development requirements.The popularity and usage of Scala are rapidly rising, evident by the ever-increasing open positions for Scala developers.
So, now that the first installment of this two fold article has been published (hence you might have an idea of what Agile Testing is not in my opinion), I’ve started feeling the pressure to explain what Agile Testing actually means to me.
Did you know that according to Statista, the number of smartphone users will reach 18.22 billion by 2025? Let’s face it, digital transformation is skyrocketing and will continue to do so. This swamps the mobile app development market with various options and gives rise to the need for the best mobile app testing tools
As a developer, checking the cross browser compatibility of your CSS properties is of utmost importance when building your website. I have often found myself excited to use a CSS feature only to discover that it’s still not supported on all browsers. Even if it is supported, the feature might be experimental and not work consistently across all browsers. Ask any front-end developer about using a CSS feature whose support is still in the experimental phase in most prominent web browsers. ????
The count of mobile users is on a steep rise. According to the research, by 2025, it is expected to reach 7.49 billion users worldwide. 70% of all US digital media time comes from mobile apps, and to your surprise, the average smartphone owner uses ten apps per day and 30 apps each month.
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!