比如有个情景是需要先返回'开始执行'然后进行执行任务。 或者 Python2 中有没有那种。。。异步的写法,类似 iOS 中的 block。
1
Zuckonit 2017-08-22 09:35:40 +08:00
yield
|
2
ihonliu 2017-08-22 09:37:01 +08:00
|
4
fanhaipeng0403 2017-08-22 09:42:20 +08:00
异步执行
|
5
Immortal 2017-08-22 09:44:59 +08:00
"开始执行"这个应该是客户端可以直接处理
服务端最终未开始执行(执行超时) 或 执行错误 再 return 给客户端才是正常处理方式? |
6
fanhaipeng0403 2017-08-22 09:46:45 +08:00
celery apply_async
|
7
bestkayle OP @fanhaipeng0403 #6 因为 celery 出了点问题刚把 celery 去掉了
|
10
siloong 2017-08-22 10:17:31 +08:00
第一反应也是 yield...
|
11
inmyfree 2017-08-22 11:06:08 +08:00
可以这样处理
把要执行的任务模块化,先添加到任务列表,然后返回 另起一个定时器或者守护线程类的,间隔 N 秒查询任务列表,有任务就执行,没有就完了 |
12
est 2017-08-22 11:08:09 +08:00
>>> def a():
>>> try: return 1 >>> except: pass >>> finally: print 'haha' >>> >>> a() haha 1 |
13
WildCat 2017-08-22 11:11:03 +08:00
yield/async await
|
16
Immortal 2017-08-22 11:39:15 +08:00
@bestkayle 不清楚你做的是 web 还是什么业务 任务队列没法准备的把每个任务状态返回给客户端吧?比如有 1,2,3 个任务在队列里,其中 2 处理失败, 怎么告诉发布 2 任务的客户端失败这个信息?如果做 tcp 业务那是没问题
|
18
bestkayle OP @Immortal #16 原来是有个进程在一直等待获取任务,重启的时候直接执行任务,现在改成了重启的时候也扔到任务队列里面去,加个重启的标签以便识别。
|
19
est 2017-08-22 11:57:30 +08:00
@bestkayle
https://www.python.org/dev/peps/pep-0333/#specification-details If the iterable returned by the application has a close() method, the server or gateway must call that method upon completion of the current request, whether the request was completed normally, or terminated early due to an error (this is to support resource release by the application). This protocol is intended to complement PEP 325's generator support, and other common iterables with close() methods. |
20
goodryb 2017-08-22 11:58:44 +08:00
为什么要返回开始执行后在跑任务,奇怪的逻辑
我理解应该是提交任务后,这个动作就结束了,后台执行任务时应该写入状态值,前端查询状态值来确认是否开始执行 |
21
bestkayle OP |
22
goodryb 2017-08-22 12:41:40 +08:00
|
23
ericls 2017-08-22 12:59:09 +08:00 via iPhone
楼主想解决 wsgi 返回 response 以后的操作?
|
24
xieranmaya 2017-08-22 13:05:11 +08:00 via Android
tryfinally
|
26
mansur 2017-08-22 13:51:08 +08:00
服务器端接收请求后生成个随机码,连同待处理的数据一块发给 redis 队列,然后把随机码返回给客户端。另外起一个后台进程一直处理这个 redies 队列的消息,处理好了存库。客户端接收到随机码后带着这个随机码向服务器端请求处理结果,如果没有结果就循环重试,用 ajax 实现即可。
|