MCPcopy
hub / github.com/aio-libs/aiohttp / run_twisted

Function run_twisted

benchmark/async.py:110–168  ·  view source on GitHub ↗
(host, port, barrier, profile)

Source from the content-addressed store, hash-verified

108
109
110def run_twisted(host, port, barrier, profile):
111
112 if 'bsd' in sys.platform or sys.platform.startswith('darwin'):
113 from twisted.internet import kqreactor
114 kqreactor.install()
115 elif sys.platform in ['win32']:
116 from twisted.internet.iocpreactor import reactor as iocpreactor
117 iocpreactor.install()
118 elif sys.platform.startswith('linux'):
119 from twisted.internet import epollreactor
120 epollreactor.install()
121 else:
122 from twisted.internet import default as defaultreactor
123 defaultreactor.install()
124
125 from twisted.web.server import Site
126 from twisted.web.resource import Resource
127 from twisted.internet import reactor
128
129 class TestResource(Resource):
130
131 def __init__(self, name):
132 super().__init__()
133 self.name = name
134 self.isLeaf = name is not None
135
136 def render_GET(self, request):
137 txt = 'Hello, ' + self.name
138 request.setHeader(b'Content-Type', b'text/plain; charset=utf-8')
139 return txt.encode('utf8')
140
141 def getChild(self, name, request):
142 return TestResource(name=name.decode('utf-8'))
143
144 class PrepareResource(Resource):
145
146 isLeaf = True
147
148 def render_GET(self, request):
149 gc.collect()
150 return b'OK'
151
152 class StopResource(Resource):
153
154 isLeaf = True
155
156 def render_GET(self, request):
157 reactor.callLater(0.1, reactor.stop)
158 return b'OK'
159
160 root = Resource()
161 root.putChild(b'test', TestResource(None))
162 root.putChild(b'prepare', PrepareResource())
163 root.putChild(b'stop', StopResource())
164 site = Site(root)
165 reactor.listenTCP(port, site, interface=host)
166 barrier.wait()
167

Callers

nothing calls this directly

Calls 6

ResourceClass · 0.85
TestResourceClass · 0.85
PrepareResourceClass · 0.85
StopResourceClass · 0.85
waitMethod · 0.80
runMethod · 0.45

Tested by

no test coverage detected