私信  •  关注

xyres

xyres 最近创建的主题
xyres 最近回复了
2 年前
回复了 xyres 创建的主题 » Django/OnetoMany在同一类中的关系

据我所知,您的要求是:

  • 一个场景可能有 只有前一幕
  • 一个场景可能有 接下来还有很多场景

你就快到了。而不是 OneToOneField 使用 ForeignKey 具有 self 参考:

previous = models.ForeignKey('self', related_name='next', ...)
6 年前
回复了 xyres 创建的主题 » 在Tornado处理程序中对json-util-dumps中的项排序

python字典不维护顺序。有一个特殊的类可以用来保存dicts中的顺序- collections.OrderedDict . 而不是使用 dict ,您需要使用 OrderedDict .

例子:

from collections import OrderedDict

doc = OrderedDict([
    ('cid', document['cid']),
    ('mac', document['mac']),
    ('category', document['category']),
    # ... other keys ...
])

docs_uplink.append(doc)
dumped = json_util.dumps(dict(data=docs_uplink))
6 年前
回复了 xyres 创建的主题 » 找不到NoreverSematch URL Django

你在用 app_name = 'chat' . 这使您的应用程序级URL在应用程序命名空间下可用。参见文档: https://docs.djangoproject.com/en/2.1/topics/http/urls/#url-namespaces-and-included-urlconfs

这应该有效:

{% url 'chat:thread' username %}
6 年前
回复了 xyres 创建的主题 » python 2龙卷风非同步方法

使用龙卷风自己的 process.Subprocess 类,它是stdlib的包装器。 subprocess.Popen .

例子:

from tornado import process

@gen.coroutine
def longProcess():
    bashCommand = "sleep 5 && echo hi"

    proc = process.Subprocess(bashCommand.split(), stdout=subprocess.PIPE)

    yield proc.wait_for_exit() # `wait_for_exit` returns a future 
                               # which you can yield. Basically, it means
                               # you can wait for the process to complete
                               # without blocking the server

    return proc.stdout.read() # return the result of the process

    # for Python 2:
    # you can't return from a generator
    # instead use this:
    # raise gen.Return(proc.stdout.read())

你不需要 while 循环。您可以将代码移到其他的协同程序中,并生成列表。 futures . 这样地:

@gen.coroutine
def main():
    futures = [longProcess() for x in range(0, 5)]

    results = yield futures # this coroutine will not move further 
                            # until every future in the list 
                            # `futures` is resolved

    for result in results:
        print(result)

    print('All futures resolved')

作为补充说明,不要使用 time.sleep . 它会阻塞整个服务器。而是使用异步等效 gen.sleep - yield gen.sleep(1)

6 年前
回复了 xyres 创建的主题 » Django:关闭选项卡时自动注销(而不是浏览器)

与姜戈毫无关系。它与浏览器如何处理cookie更相关。

如果一个cookie没有过期日期,会发生什么?( Expires 属性)或 Max-Age ,浏览器将它们视为 会话cookie 。这意味着当您退出时浏览器将删除该cookie 浏览器会话 即当您关闭浏览器时。

由于关闭标签不等于退出浏览器,浏览器不会删除会话cookie。

6 年前
回复了 xyres 创建的主题 » 在Jupyter笔记本中运行Tornado服务器

您可以使用 %%script --bg 魔法命令。选择权 --bg 告诉Jupyter在后台运行当前单元格的代码。

只需在一个单元中使用magic命令创建一个Tornado服务器并运行该单元。

例子:

%%script python --bg

import tornado.ioloop
import tornado.web

class MainHandler(tornado.web.RequestHandler):
    def get(self):
        self.write("Hello, world")

def make_app():
    return tornado.web.Application([
        (r"/", MainHandler),
    ])

loop = tornado.ioloop.IOLoop.current()

app = make_app()
app.listen(8000) # 8888 was being used by jupyter in my case

loop.start()

然后你可以用 requests 在连接到服务器的单独单元中:

import requests

print(requests.get("http://localhost:8000"))

# prints <Response [200]>

这里需要注意的一点是,如果您停止/中断任何单元格上的内核,后台脚本也将停止。所以您必须再次运行这个单元才能启动服务器。