【问题标题】:Specifying timeout while fetching/putting data in memcache (django)在 memcache (django) 中获取/放入数据时指定超时
【发布时间】:2012-04-17 09:52:20
【问题描述】:

我有一个基于 django 的 http 服务器,我使用 django.core.cache.backends.memcached.MemcachedCache 作为客户端库来访问 memcache。我想知道我们是否可以设置超时或其他东西(比如 500 毫秒),以便在 500 毫秒内无法访问缓存时,对 memcached 的调用返回 False。我们打电话给数据库。有没有这样的设置可以做到这一点?

【问题讨论】:

    标签: python django timeout memcached


    【解决方案1】:

    以前没有尝试过,但是您可以使用线程并为函数调用缓存设置超时。作为示例,请忽略此链接主体中提供的示例,但请查看 Jim Carroll 的评论:

    http://code.activestate.com/recipes/534115-function-timeout/

    适用于您可能会使用的东西:

    from threading import Timer
    import thread, time, sys
    
    def timeout():
        thread.interrupt_main()
    
    try:
        Timer(0.5, timeout).start()
        cache.get(stuff)
    except:
        print "Use a function to grab it from the database!"
    

    我现在没有时间测试它,但我关心的是 Django 本身是否是线程化的,如果是这样,中断主线程是你真正想要做的吗?无论哪种方式,这都是一个潜在的起点。我确实寻找了一个允许这样做的配置选项,但一无所获。

    【讨论】:

    • django 确实会有问题,因为它没有线程。我正在查看代码,并在 python memcache 客户端 (memcache.py) 中使用 _SOCKET_TIMEOUT 指定了套接字超时。默认设置为 3 秒。这对我来说有点太高了。可能我将不得不修改这个值。但是很奇怪,python memcache 客户端并没有提供任何方式来自定义这个超时时间。
    • YES,你可以指定超时时间。将参数socket_timeout传入memcache的Client(),如:from memcache import Client; c = Client(['localhost'], socket_timeout=10.5)
    猜你喜欢
    • 1970-01-01
    • 2012-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-27
    • 2021-03-19
    • 2021-10-07
    相关资源
    最近更新 更多