我在Celery上运行了很长时间的任务。完成后,我想向浏览器发送一条消息,指示它刷新页面。为此,我想使用频道V2。
从文档中不清楚如何实现这一点。
这是我在后台任务中所拥有的,但我不确定如何设置消费者
Microsoft (R) Windows (R) Resource Compiler Version 6.3.9600.17336
Copyright (C) Microsoft Corporation. All rights reserved.
EMU_CC obj/win32/opt/smp/beam_emu.o
In file included from beam/erl_threads.h:265:0,
from beam/erl_smp.h:28,
from beam/sys.h:491,
from beam/beam_emu.c:26:
../include/internal/ethread.h:381:2: error: #error "No native ethread implementation found. If you want to use fallbacks you have to disable native ethread support with configure."
#error "No native ethread implementation found. If you want to use fallbacks you have to disable native ethread support with configure."
^~~~~
In file included from beam/beam_load.h:25:0,
from beam/export.h:81,
from beam/global.h:33,
from beam/beam_emu.c:28:
beam/erl_process.h: In function 'erts_get_runq_proc':
beam/erl_process.h:2159:12: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
return (ErtsRunQueue *) erts_atomic_read_nob(&p->run_queue);
^
In file included from beam/erl_process.h:2347:0,
from beam/beam_load.h:25,
from beam/export.h:81,
from beam/global.h:33,
from beam/beam_emu.c:28:
beam/erl_process_lock.h: In function 'erts_pix2proc':
beam/erl_process_lock.h:939:12: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
proc = (Process *) erts_ptab_pix2intptr_nob(&erts_proc, ix);
^
beam/erl_process_lock.h: In function 'erts_proc_lookup_raw':
beam/erl_process_lock.h:952:12: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
proc = (Process *) erts_ptab_pix2intptr_ddrb(&erts_proc,
^
In file included from beam/erl_port.h:30:0,
from beam/global.h:44,
from beam/beam_emu.c:28:
beam/erl_port_task.h: In function 'erts_port_task_handle_init':
beam/erl_port_task.h:154:36: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
erts_smp_atomic_init_nob(pthp, (erts_aint_t) NULL);
^
beam/erl_port_task.h: In function 'erts_port_task_is_scheduled':
beam/erl_port_task.h:160:13: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
return ((void *) erts_smp_atomic_read_acqb(pthp)) != NULL;
^
In file included from beam/global.h:44:0,
from beam/beam_emu.c:28:
beam/erl_port.h: In function 'erts_port_runq':
beam/erl_port.h:221:11: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
rq1 = (ErtsRunQueue *) erts_smp_atomic_read_nob(&prt->run_queue);
^
beam/erl_port.h:226:8: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
rq2 = (ErtsRunQueue *) erts_smp_atomic_read_nob(&prt->run_queue);
^
beam/erl_port.h: In function 'erts_pix2port':
beam/erl_port.h:475:11: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
prt = (Port *) erts_ptab_pix2intptr_nob(&erts_port, ix);
^
beam/erl_port.h: In function 'erts_port_lookup_raw':
beam/erl_port.h:489:11: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
prt = (Port *) erts_ptab_pix2intptr_ddrb(&erts_port,
^
beam/erl_port.h: In function 'erts_id2port':
beam/erl_port.h:518:11: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
prt = (Port *) erts_ptab_pix2intptr_ddrb(&erts_port,
^
beam/erl_port.h: In function 'erts_id2port_sflgs':
beam/erl_port.h:552:11: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
prt = (Port *) erts_ptab_pix2intptr_ddrb(&erts_port,
^
beam/erl_port.h: In function 'erts_thr_id2port_sflgs':
beam/erl_port.h:611:11: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
prt = (Port *) erts_ptab_pix2intptr_ddrb(&erts_port,
^
beam/beam_emu.c: In function 'process_main':
beam/beam_emu.c:768:17: warning: right shift count >= width of type [-Wshift-count-overflow]
if ((_res >> (8*sizeof(Uint))) != 0) { Fail; } \
^
beam/beam_emu.c:4068:7: note: in expansion of macro 'BsSafeMul'
BsSafeMul(Arg2, Unit, goto system_limit, tmp_arg1);
^~~~~~~~~
beam/beam_emu.c:768:17: warning: right shift count >= width of type [-Wshift-count-overflow]
if ((_res >> (8*sizeof(Uint))) != 0) { Fail; } \
^
beam/beam_emu.c:4129:3: note: in expansion of macro 'BsSafeMul'
BsSafeMul(b, Unit, goto system_limit, c);
^~~~~~~~~
beam/beam_emu.c:768:17: warning: right shift count >= width of type [-Wshift-count-overflow]
if ((_res >> (8*sizeof(Uint))) != 0) { Fail; } \
^
beam/beam_emu.c:784:7: note: in expansion of macro 'BsSafeMul'
BsSafeMul(_uint_size, Unit, Fail, Target); \
^~~~~~~~~
beam/beam_emu.c:4649:6: note: in expansion of macro 'BsGetFieldSize'
BsGetFieldSize(tmp_arg2, (flags >> 3), ClauseFail(), size);
^~~~~~~~~~~~~~
beam/beam_emu.c:768:17: warning: right shift count >= width of type [-Wshift-count-overflow]
if ((_res >> (8*sizeof(Uint))) != 0) { Fail; } \
^
beam/beam_emu.c:784:7: note: in expansion of macro 'BsSafeMul'
BsSafeMul(_uint_size, Unit, Fail, Target); \
^~~~~~~~~
beam/beam_emu.c:835:5: note: in expansion of macro 'BsGetFieldSize'
BsGetFieldSize(Sz, ((Flags) >> 3), Fail, _size); \
^~~~~~~~~~~~~~
win32/opt/smp/beam_cold.h:12:5: note: in expansion of macro 'BsGetBinary_2'
BsGetBinary_2(r(0), Arg(1), targ1, Arg(3), dst, StoreResult, ClauseFail());
^~~~~~~~~~~~~
beam/beam_emu.c:768:17: warning: right shift count >= width of type [-Wshift-count-overflow]
if ((_res >> (8*sizeof(Uint))) != 0) { Fail; } \
^
beam/beam_emu.c:784:7: note: in expansion of macro 'BsSafeMul'
BsSafeMul(_uint_size, Unit, Fail, Target); \
^~~~~~~~~
beam/beam_emu.c:835:5: note: in expansion of macro 'BsGetFieldSize'
BsGetFieldSize(Sz, ((Flags) >> 3), Fail, _size); \
^~~~~~~~~~~~~~
win32/opt/smp/beam_cold.h:22:5: note: in expansion of macro 'BsGetBinary_2'
BsGetBinary_2(xb(Arg(1)), Arg(2), targ1, Arg(4), dst, StoreResult, ClauseFail());
^~~~~~~~~~~~~
beam/beam_emu.c:768:17: warning: right shift count >= width of type [-Wshift-count-overflow]
if ((_res >> (8*sizeof(Uint))) != 0) { Fail; } \
^
beam/beam_emu.c:784:7: note: in expansion of macro 'BsSafeMul'
BsSafeMul(_uint_size, Unit, Fail, Target); \
^~~~~~~~~
beam/beam_emu.c:870:4: note: in expansion of macro 'BsGetFieldSize'
BsGetFieldSize(Bits, Unit, Fail, _size); \
^~~~~~~~~~~~~~
win32/opt/smp/beam_cold.h:86:5: note: in expansion of macro 'BsSkipBits2'
BsSkipBits2(r(0), xb(Arg(1)), Arg(2), ClauseFail());
^~~~~~~~~~~
beam/beam_emu.c:768:17: warning: right shift count >= width of type [-Wshift-count-overflow]
if ((_res >> (8*sizeof(Uint))) != 0) { Fail; } \
^
beam/beam_emu.c:784:7: note: in expansion of macro 'BsSafeMul'
BsSafeMul(_uint_size, Unit, Fail, Target); \
^~~~~~~~~
beam/beam_emu.c:870:4: note: in expansion of macro 'BsGetFieldSize'
BsGetFieldSize(Bits, Unit, Fail, _size); \
^~~~~~~~~~~~~~
win32/opt/smp/beam_cold.h:94:5: note: in expansion of macro 'BsSkipBits2'
BsSkipBits2(r(0), yb(Arg(1)), Arg(2), ClauseFail());
^~~~~~~~~~~
beam/beam_emu.c:768:17: warning: right shift count >= width of type [-Wshift-count-overflow]
if ((_res >> (8*sizeof(Uint))) != 0) { Fail; } \
^
beam/beam_emu.c:784:7: note: in expansion of macro 'BsSafeMul'
BsSafeMul(_uint_size, Unit, Fail, Target); \
^~~~~~~~~
beam/beam_emu.c:870:4: note: in expansion of macro 'BsGetFieldSize'
BsGetFieldSize(Bits, Unit, Fail, _size); \
^~~~~~~~~~~~~~
win32/opt/smp/beam_cold.h:102:5: note: in expansion of macro 'BsSkipBits2'
BsSkipBits2(xb(Arg(1)), r(0), Arg(2), ClauseFail());
^~~~~~~~~~~
beam/beam_emu.c:768:17: warning: right shift count >= width of type [-Wshift-count-overflow]
if ((_res >> (8*sizeof(Uint))) != 0) { Fail; } \
^
beam/beam_emu.c:784:7: note: in expansion of macro 'BsSafeMul'
BsSafeMul(_uint_size, Unit, Fail, Target); \
^~~~~~~~~
beam/beam_emu.c:870:4: note: in expansion of macro 'BsGetFieldSize'
BsGetFieldSize(Bits, Unit, Fail, _size); \
^~~~~~~~~~~~~~
win32/opt/smp/beam_cold.h:110:5: note: in expansion of macro 'BsSkipBits2'
BsSkipBits2(xb(Arg(1)), xb(Arg(2)), Arg(3), ClauseFail());
^~~~~~~~~~~
beam/beam_emu.c:768:17: warning: right shift count >= width of type [-Wshift-count-overflow]
if ((_res >> (8*sizeof(Uint))) != 0) { Fail; } \
^
beam/beam_emu.c:784:7: note: in expansion of macro 'BsSafeMul'
BsSafeMul(_uint_size, Unit, Fail, Target); \
^~~~~~~~~
beam/beam_emu.c:870:4: note: in expansion of macro 'BsGetFieldSize'
BsGetFieldSize(Bits, Unit, Fail, _size); \
^~~~~~~~~~~~~~
win32/opt/smp/beam_cold.h:118:5: note: in expansion of macro 'BsSkipBits2'
BsSkipBits2(xb(Arg(1)), yb(Arg(2)), Arg(3), ClauseFail());
^~~~~~~~~~~
make[3]: *** [win32/Makefile:672: obj/win32/opt/smp/beam_emu.o] Error 1
make[3]: Leaving directory '/cygdrive/c/werl/otp_src_18.3/erts/emulator'
make[2]: *** [/cygdrive/c/werl/otp_src_18.3/make/run_make.mk:35: opt] Error 2
make[2]: Leaving directory '/cygdrive/c/werl/otp_src_18.3/erts/emulator'
make[1]: *** [Makefile:61: smp] Error 2
make[1]: Leaving directory '/cygdrive/c/werl/otp_src_18.3/erts'
make: *** [Makefile:444: emulator] Error 2
答案 0 :(得分:2)
class ReloadConsumer(WebsocketConsumer):
def connect(self):
self.group_name = self.scope['user']
print(self.group_name) # use this for debugging not sure what the scope returns
# Join group
async_to_sync(self.channel_layer.group_add)(
self.group_name,
self.channel_name
)
self.accept()
def disconnect(self, close_code):
# Leave group
async_to_sync(self.channel_layer.group_discard)(
self.group_name,
self.channel_name
)
def reload_page(self, event):
# Send message to WebSocket
self.send(text_data=json.dumps({
'reload': True
}))
self.disconnect()
然后,当您的芹菜任务完成后,您将向相关组发送reload_page消息。
@shared_task
def my_task():
... do your task ...
group_name = get_user_name() # Find out way to get same as what is printed on connect()
channel_layer = get_channel_layer()
# Trigger reload message sent to group
async_to_sync(channel_layer.group_send)(
group_name,
{'type': 'reload_page'}
)
一旦你可以在用户启动芹菜任务时成功创建一个组,然后在完成时向用户的组发送一条消息就像添加这个脚本一样简单:
webSocket.onmessage = function() {
window.location.reload();
}