我一直在玩Tornado聊天演示.在一个偶然一瞥,似乎像new_messages方法不是线程安全的-这似乎是项目可能会被添加到waiters阵列,同时在同一个阵列正在被迭代for循环.
这个演示不是线程安全的吗?或者,它是否是线程安全的,因为Python set对象本身是线程安全的? Python 对象是否set安全?我似乎在这个问题上发现了相互矛盾的观点(这个词set难以在Google中有效地搜索!)
加分点 - 为什么waiters数组set在迭代结束时设置为新的而不是清空set?
我碰到node.js和python's tornadoVS的Apache.
他们说 :
Node.js&tornado实际上在线程上进行事件循环,单个线程可以处理许多连接.我不明白逻辑上是一个线程的孩子.在计算机科学术语中:
现在,
什么event loop在线程下如何工作?
如何在一个线程的控制下处理不同的连接?
更新:
我的意思是如果在1个线程下有3个套接字的通信,1个线程如何与3个套接字通信而不让任何人等待?
我使用yield和task来异步获取四个jsons:
@gen.engine
def get_user_data(self, sn, snid, fast_withdrawals):
end_timestamp = time.time()
start_timestamp = end_timestamp - CONFIG.LOYALITY_LEVELS.PERIOD
active_apps_response, total_payments_response, payments_for_period_response, withdrawals_response = yield [
gen.Task(self.http_client.fetch, self.__get_active_apps_url(sn, snid)), gen.Task(self.http_client.fetch, self.__get_total_payments_url(sn, snid)),
gen.Task(self.http_client.fetch, self.__get_payments_sum_for_period_url(sn, snid, start_timestamp, end_timestamp)),
gen.Task(self.http_client.fetch, self.__get_total_withdrawals_url(sn, snid, fast_withdrawals))
]
active_apps = self.__active_apps_handler(active_apps_response)
total_payments = self.__get_total_payments_handler(total_payments_response)
payments_for_period = self.__payments_sum_for_period_handler(payments_for_period_response)
withdrawals = self.__get_total_withdrawals_handler(withdrawals_response)
yield gen.Return(active_apps, total_payments, payments_for_period, withdrawals)
Run Code Online (Sandbox Code Playgroud)
但是如果我使用yield而不是返回上层函数也变成了生成器而且我也不能使用return.那么,如何在没有调用函数生成器的情况下从龙卷风函数返回结果呢?我正在使用Python 2.7
我的代码是用龙卷风编写的,我想让它像apache或nginx一样工作,就是这样
即使我关闭shell,我也尝试过nohup命令使其工作.有用.但我想知道是否有更清洁的选择?
我有一种情况,css背景图像无法加载我的webapp.奇怪的是,他们正在使用绝对路径(以"/"开头)并且路径是正确的.
当我期望带有DOM Inspector的元素时,firefox会给出错误"无法加载图像".但是,如果我单击该链接,图像将在新选项卡中打开,显示路径正确.
演示:https://qa.hubble.in/#subscriptions
在此页面的右上角,有五个正方形.这是主要的应用程序导航菜单,应该有图标.如果右键单击并在最左侧的方块上选择"检查元素",则检查器将在"a title = Dashboard"元素上打开.如果你去它的父母,"li class = nav_dashboard",你可以看到我在说什么.
检查器中的CSS显示:
ul.nav-tabs li.nav_dashboard, li.nav_dashboard.active {
background-image: url('/images/icons/liveViewInactive.svg');
}
Run Code Online (Sandbox Code Playgroud)
然而,令人烦恼的是,如果您在浏览器中打开https://qa.hubble.in/images/icons/liveViewInactive.svg,则可以看到该图标.从CSS指定时为什么不加载?是否有一些我不知道的晦涩难懂的规则?
更多信息:
网络服务器是龙卷风http服务器,我以前没有任何这样的问题.我真的看不出任何理由为什么在CSS请求时不会提供图像,就像它们一直有的那样.
相同的代码在我的开发环境中运行良好:https://darrel.hubble.in/#subscriptions,但是我害怕将任何东西都投入生产,直到我在qa环境中解决这个问题.两者之间的唯一区别是darrel.hubble.in,一个有效的,在一个elasticbeanstalk实例实例中运行,而qa.hubble.in,一个有这个神秘问题的,正在一个常规的amazon ec2实例中运行.很难想象这会导致这种情况.
编辑(解决方案):
这似乎是龙卷风StaticFileHandler为svg文件提供内容类型的方式中的一个错误.
为了解决这个问题,我将tornado.web.statcifilehandler子类化了(由于其他原因我还是继承了它),并添加了这个:
if ".svg" in self.absolute_path:
self.set_header("Content-Type", "image/svg+xml")
Run Code Online (Sandbox Code Playgroud) 关于龙卷风的stackoverflow有几个问题我仍然没有找到我的问题的答案我有一个大文本文件,我希望迭代并发送每一行作为POST http请求.我希望将其作为异步(我需要快速),然后检查请求的响应.
我有类似的东西
http_client = httpclient.AsyncHTTPClient()
with open(filename) as log_file:
for line in log_file:
request = httpclient.HTTPRequest(self.destination,method="POST",headers=self.headers,body=json.dumps(line))
response = http_client.fetch(request, callback=self.handle_request)
Run Code Online (Sandbox Code Playgroud)
看看tcpdump这没有做任何我得到的是一个严重的"期货"对象我也尝试将fetch命令放在"yield"中,然后在方法上使用@ gen.coroutine装饰器时迭代它.这没有帮助.任何人都可以告诉我,我做错了什么?
谢谢!
如果我有以下内容:
@tornado.gen.coroutine
def first(x):
#
# do stuff
for i in I:
tornado.ioloop.IOLoop.current().spawn_callback(func,i)
tornado.ioloop.IOLoop.current().spawn_callback(func2,z)
yield first(xxx)
Run Code Online (Sandbox Code Playgroud)
我可以保证for循环中的所有生成函数都会在最后一次生成func2()回调之前运行吗?
我们有一个Python 2项目,我们积极使用协同程序.我们找不到关于协程内部异常处理的任何指南.
例如,在这里龙卷风的主要开发人员提到coroutines should never raise an exception,但目前尚不清楚为什么.看起来这种方法可以在Tornado.web本身中使用并大量使用:
https://github.com/tornadoweb/tornado/blob/master/demos/blog/blog.py#L180
class AuthCreateHandler(BaseHandler):
def get(self):
self.render("create_author.html")
@gen.coroutine
def post(self):
if self.any_author_exists():
raise tornado.web.HTTPError(400, "author already created")
hashed_password = yield executor.submit(
bcrypt.hashpw, tornado.escape.utf8(self.get_argument("password")),
bcrypt.gensalt())
Run Code Online (Sandbox Code Playgroud)
tornado.web.HTTPError只是扩展了基本的Exception类.另外,这里的讨论https://github.com/tornadoweb/tornado/issues/759#issuecomment-91817197建议在协程内提高异常是合适的.
同样在这里,活跃的龙卷风贡献者建议提高例外是好的:
class PostHandler(tornado.web.RequestHandler):
@gen.coroutine
def get(self, slug):
post = yield db.posts.find_one({'slug': slug})
if not post:
raise tornado.web.HTTPError(404)
self.render('post.html', post=post)
Run Code Online (Sandbox Code Playgroud)
在Tornado协同程序中提出异常是否有任何缺点,或者我们应该raise gen.Return(exception_object)吗?
我有一个我不明白原因的问题...基本上,我无法在Tornado / Python上设置cookie,无论是常规cookie还是安全cookie。我的代码:
class API(BaseHandler):
def get(self):
self.set_secure_cookie("a_cookie", "a_value")
a_cookie = self.get_secure_cookie("a_cookie")
print(">>> a_cookie ", a_cookie)
self.set_cookie("a_cookie", "a_value")
a_cookie = self.get_cookie("a_cookie")
print(">>> a_cookie ", a_cookie)
Run Code Online (Sandbox Code Playgroud)
输出为:
>>> a_cookie None
>>> a_cookie None
Run Code Online (Sandbox Code Playgroud)
BaseHandler类扩展了tornado.web.RequestHandler。
关于安全cookie,我设置cookie_secret。
我不了解设置Cookie时无法获取它的原因。我相信,我在下面的网站规则(http://www.tornadoweb.org/en/stable/guide/security.html,https://technobeans.com/2012/08/07/tornado-cookies/)
tornado ×10
python ×7
cookies ×1
css ×1
daemon ×1
event-loop ×1
eventmachine ×1
events ×1
generator ×1
http ×1
javascript ×1
linux ×1
nginx ×1
node.js ×1
yield ×1