def twisted_callback2(): print "twisted (shutdown)", kaa.is_mainthread() if not stop_on_exit: return if method == 2: kaa.main.stop() else: reactor.stop()
def thread_test(): """ Test with a generator in a thread. Each value from yield is send to the generator. """ print "Is main thread:", kaa.is_mainthread() yield 1 yield 2 yield 3
def coroutine_test(): """ Test with a coroutine generator. The coroutine has to raise GeneratorExit to be stopped. Each value yielded will be send to the generator. """ print "Is main thread:", kaa.is_mainthread() yield kaa.NotFinished yield 1 x = yield async () yield x + 1 yield 2 yield kaa.NotFinished yield 3
def newfunc(self, *args, **kwargs): # FIXME: should get supported charsets from request charset = cherrypy.config.get('charset', 'utf-8') # Set content-type before calling handler to give handler a chance # to override it. if not json: cherrypy.serving.response.headers['Content-Type'] = '%s; charset=%s' % (content_type, charset) if engine == 'raw' and template: from cherrypy.lib.static import serve_file return serve_file(template, content_type=content_type) # We pass this context as the first arg to each handler. The # reason is that in cherrypy, the request and response objects are # dependent on which thread they are being accessed from. So if # the handler wants to modify the response.headers dict and that # handler is being called from the main thread, it cannot access # cherrypy.response.headers because this is the response object for # the main thread, not the current thread. class ctx: request = cherrypy.serving.request response = cherrypy.serving.response if mainloop and not kaa.is_mainthread(): result = kaa.MainThreadCallable(func)(self, ctx, *args, **kwargs).wait() else: result = func(self, ctx, *args, **kwargs) if cache is not None: seconds = int(cache * 60 * 60) if seconds != 0: gmtime = time.gmtime(time.time() + seconds) ctx.response.headers['Expires'] = time.strftime('%a, %d %b %Y %H:%M:%S GMT', gmtime) ctx.response.headers['Cache-Control'] = 'public, max-age=%d' % seconds else: ctx.response.headers['Cache-Control'] = 'no-cache, must-revalidate' if json: # Handle JSON response. import json as pyjson # Do not set charset here, it breaks IE. Yes, seriously. ctx.response.headers['Content-Type'] = 'text/plain' return '{}&& ' + pyjson.write(result).encode(charset) if not template: return strutils.to_unicode(result).encode(charset) # Engine should return a string encoded with charset return engine.parse(template, charset, result)
def twisted_callback1(): print "twisted", kaa.is_mainthread()
def kaa_callback(): print 'kaa', kaa.is_mainthread()
def foo(self): import time time.sleep(0.4) return kaa.is_mainthread()
def test(self): r = yield self.foo() print 'foo', kaa.is_mainthread(), r
def test2(self, x): return x, kaa.is_mainthread()
def bar(self): print 'bar is mainthread:', is_mainthread()
def foo(self): print 'foo is mainthread:', is_mainthread() self.bar()
def twisted_callback2(): print "twisted (shutdown)", kaa.is_mainthread() # you can either call reactor.stop or kaa.main.stop reactor.stop()
def new_device(dev): print kaa.is_mainthread() print dev.udi dev.mount() kaa.OneShotTimer(dev.eject).start(1)