Celery上手使用遇到困难,server卡在 task.delay(),似乎进不去任务方法 10C

作为一个celery的初学者,我试图在我的简单的flask web项目中添加celery的异步特性。在安装并启动celery、redis和相关的python pip之后,我在一开始就遇到了卡在task.delay()的问题。很沮丧,能帮我一下吗?



在flask项目中有app.py和tasks.py。(一开始我把所有东西都写在app.py中,然后我把celery对象分开,但没有任何帮助)



tasks.py:

from celery import Celery

celery = Celery('app', broker='redis://localhost:6379/0', backend='redis://localhost:6379/1')


@celery.task
def modeling_task():
    print('yes')



app.py:

@app.route('/train', methods=['GET', 'POST'])
def train():
    if request.method == "GET":
        return render_template('train.html')
    else:  # when request POST
        # check if the post request has the file part
        if 'file' not in request.files:
            return jsonify({"code": 500, "status": 'No file is uploaded!'})

        if file and allowed_file(file.filename):
            print(request.files['file'].filename)
            print('before task')
            task = modeling_task.delay()
            print('after task')

            return jsonify({"code": 200, "status": 'model training', "task_id": task.id})

        return 'Uploading file is wrong'



按理说,当server接收到前端上传的文件时,控制台应该输出这些

Car_TEST.csv
before task
yes
after task



但实际上输出的是这样

 * Detected change in 'C:\\Users\\headmaster\\Desktop\\WEB\\tasks.py', reloading
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 308-608-393
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
127.0.0.1 - - [22/Apr/2020 11:39:22] "GET /train HTTP/1.1" 200 -
Car_TEST.csv
before task

然后server就一直卡在这里了



好像"task = modeling_task.delay()" 根本就没执行



是因为它有执行,然后“yes”被打印在别的地方了吗?
为什么会一直卡在那呢?



下面是celery的report

(base) C:\Users\headmaster\Desktop\WEB>celery -A tasks report

software -> celery:4.4.2 (cliffs) kombu:4.6.8 py:3.7.4
            billiard:3.6.3.0 redis:3.4.1
platform -> system:Windows arch:64bit, WindowsPE
            kernel version:10 imp:CPython
loader   -> celery.loaders.app.AppLoader
settings -> transport:redis results:redis://localhost:6379/1

broker_url: 'redis://localhost:6379/0'
result_backend: 'redis://localhost:6379/1'



celery的运行状态:

(base) C:\Users\headmaster\Desktop\WEB>celery worker -A tasks.celery --loglevel=info

 -------------- celery@LAPTOP-KLKJCK2F v4.4.2 (cliffs)
--- ***** -----
-- ******* ---- Windows-10-10.0.18362-SP0 2020-04-22 10:55:13
- *** --- * ---
- ** ---------- [config]
- ** ---------- .> app:         app:0x2526977ac08
- ** ---------- .> transport:   redis://localhost:6379/0
- ** ---------- .> results:     redis://localhost:6379/1
- *** --- * --- .> concurrency: 4 (prefork)
-- ******* ---- .> task events: OFF (enable -E to monitor tasks in this worker)
--- ***** -----
 -------------- [queues]
                .> celery           exchange=celery(direct) key=celery


[tasks]
  . tasks.modeling_task

[2020-04-22 10:55:15,501: INFO/SpawnPoolWorker-1] child process 6692 calling self.run()
[2020-04-22 10:55:15,511: INFO/SpawnPoolWorker-2] child process 17736 calling self.run()
[2020-04-22 10:55:15,518: INFO/SpawnPoolWorker-3] child process 16320 calling self.run()
[2020-04-22 10:55:15,571: INFO/SpawnPoolWorker-4] child process 5608 calling self.run()
[2020-04-22 10:55:15,972: INFO/MainProcess] Connected to redis://localhost:6379/0
[2020-04-22 10:55:20,332: INFO/MainProcess] mingle: searching for neighbors
[2020-04-22 10:55:27,369: INFO/MainProcess] mingle: all alone
[2020-04-22 10:55:35,402: INFO/MainProcess] celery@LAPTOP-KLKJCK2F ready.



redis的启动状态:

PS D:\Applications\Redis> redis-server redis.conf
                _._
           _.-``__ ''-._
      _.-``    `.  `_.  ''-._           Redis 3.2.100 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 21420
  `-._    `-._  `-./  _.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |           http://redis.io
  `-._    `-._`-.__.-'_.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |
  `-._    `-._`-.__.-'_.-'    _.-'
      `-._    `-.__.-'    _.-'
          `-._        _.-'
              `-.__.-'

[21420] 22 Apr 10:38:22.506 # Server started, Redis version 3.2.100
[21420] 22 Apr 10:38:22.508 * DB loaded from disk: 0.000 seconds
[21420] 22 Apr 10:38:22.508 * The server is now ready to accept connections on port 6379
[21420] 22 Apr 10:43:23.025 * 10 changes in 300 seconds. Saving...
[21420] 22 Apr 10:43:23.030 * Background saving started by pid 6204
[21420] 22 Apr 10:43:23.231 # fork operation complete
[21420] 22 Apr 10:43:23.232 * Background saving terminated with success
[21420] 22 Apr 10:51:18.099 * 10 changes in 300 seconds. Saving...
[21420] 22 Apr 10:51:18.103 * Background saving started by pid 10116
[21420] 22 Apr 10:51:18.305 # fork operation complete
[21420] 22 Apr 10:51:18.306 * Background saving terminated with success
[21420] 22 Apr 10:56:19.022 * 10 changes in 300 seconds. Saving...
[21420] 22 Apr 10:56:19.026 * Background saving started by pid 11748
[21420] 22 Apr 10:56:19.227 # fork operation complete
[21420] 22 Apr 10:56:19.227 * Background saving terminated with success
a984314524
圈奶特 回复九阳神剑: 这个问题解决了吗。。。求解答
大约一个月之前 回复
a984314524
圈奶特 回复九阳神剑: 这个问题解决了吗。。。求解答
大约一个月之前 回复
qq_42286266
九阳神剑 这个问题解决了吗。。。求解答
2 个月之前 回复

1个回答

qq_37410417
pycharm专业版 您好,好像说的不是一回事啊...
4 个月之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问