perfumescent 2020-04-22 12:22 采纳率: 0%
浏览 1490
已结题

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

作为一个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
  • 写回答

1条回答 默认 最新

  • 关注
    评论

报告相同问题?

悬赏问题

  • ¥50 comsol稳态求解器 找不到解,奇异矩阵有1个空方程返回的解不收敛。没有返回所有参数步长;pid控制
  • ¥15 怎么让wx群机器人发送音乐
  • ¥15 fesafe材料库问题
  • ¥35 beats蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功