代码之家  ›  专栏  ›  技术社区  ›  nz_21

PythonRedis队列:文档中的简单示例不起作用

  •  1
  • nz_21  · 技术社区  · 7 年前

    我有两个文件,完全是复制粘贴的 http://python-rq.org/docs/ :

        from rq import Queue
        from redis import Redis
        from somewhere import count_words_at_url
        import time
    
        # Tell RQ what Redis connection to use
        redis_conn = Redis()
        q = Queue(connection=redis_conn)  # no args implies the default queue
    
        print(redis_conn)
        # Delay execution of count_words_at_url('http://nvie.com')
        job = q.enqueue(count_words_at_url, 'http://nvie.com')
        print(job.result)   # => None
    
        # Now, wait a while, until the worker is finished
        time.sleep(10)
        print(job.result)   # => 889
    

    某个地方

    import requests
    def count_words_at_url(url):
        print("hello?")
        resp = requests.get(url)
        return len(resp.text.split())
    

    app.py ,我得到的输出是2个无值,而根据文档我应该得到889。

    我不太明白为什么会发生这种事。我的超时时间是10秒,比文档中的时间长,所以我希望作业已经完成。

    1 回复  |  直到 7 年前
        1
  •  1
  •   Denny Wang    7 年前
    1. Redis服务器正在运行吗?

      服务redis服务器状态

    2. rq工人正在运行吗?

      rq信息

    如果没有工人在运行,则

    rq worker # run this under the same directory of your project
    18:44:54 RQ worker 'rq:worker:ubuntu.45276' started, version 0.12.0
    18:44:54 *** Listening on default...
    18:44:54 Cleaning registries for queue: default
    
    1. 用更简单的函数替换\u url处的\u count\u words\u,如

      def just_mock(url): 返回“计算{}的字数是???”。格式(url)