channels get url 参数没有空格
channels get url parameter without spaces
嗨,我正在摆弄 django-channels 2,想获取 URL 参数并在函数中使用它,但它似乎包含 space 或类似的东西,这会导致我的功能有问题。我的函数有 1 个参数,但是当我尝试在其中传递 URL 参数时,出现以下错误 task_status() takes 1 positional argument but 2 were given
。当我尝试打印 URL 时,我可以看到它打印了正确的值,但也创建了一个新行。
有没有办法只获取 URL 参数并能够直接在函数中使用它?
consumers.py
class ChatConsumer(AsyncConsumer):
async def websocket_connect(self, event):
await self.send({
"type": "websocket.accept"
})
user = self.scope['user']
get_task_id = self.scope['url_route']['kwargs']['task_id']
print(get_task_id)
get_info = await self.task_status(get_task_id)
print(get_info)
await self.send({
"type": "websocket.send",
"text": "hey"
})
async def websocket_receive(self, event):
print("receive", event)
async def websocket_disconnect(self, event):
print("disconnected", event)
def task_status(task_id):
command = "golemcli tasks show {}".format(task_id)
taskoutput = subprocess.getoutput(command)
print(taskoutput)
routing.py
from django.urls import re_path
from . import consumers
websocket_urlpatterns = [
re_path(r'ws/dashboard/task/(?P<task_id>[0-9a-f-]+)', consumers.ChatConsumer),
]
网站URLhttp://localhost:8000/dashboard/task/aa3c6c12-5446-11ea-b237-1e0f691c9a55
你的 task_status
是一种方法,所以它的第一个扩充应该是 self
,python 总是在你调用函数时将这个扩充添加到函数中,这就是你出错的原因.它使用 self
和 task_id
调用方法
要解决这个问题,您应该这样定义您的方法:
def task_status(self, task_id):
....
另一方面,您在 task_status
方法中同步等待,这将阻止整个服务器处理任何其他流量。
您应该将 task_status
设为 async
方法,然后使用异步子进程命令启动并 await
输出 https://docs.python.org/3/library/asyncio-subprocess.html#examples
嗨,我正在摆弄 django-channels 2,想获取 URL 参数并在函数中使用它,但它似乎包含 space 或类似的东西,这会导致我的功能有问题。我的函数有 1 个参数,但是当我尝试在其中传递 URL 参数时,出现以下错误 task_status() takes 1 positional argument but 2 were given
。当我尝试打印 URL 时,我可以看到它打印了正确的值,但也创建了一个新行。
有没有办法只获取 URL 参数并能够直接在函数中使用它?
consumers.py
class ChatConsumer(AsyncConsumer):
async def websocket_connect(self, event):
await self.send({
"type": "websocket.accept"
})
user = self.scope['user']
get_task_id = self.scope['url_route']['kwargs']['task_id']
print(get_task_id)
get_info = await self.task_status(get_task_id)
print(get_info)
await self.send({
"type": "websocket.send",
"text": "hey"
})
async def websocket_receive(self, event):
print("receive", event)
async def websocket_disconnect(self, event):
print("disconnected", event)
def task_status(task_id):
command = "golemcli tasks show {}".format(task_id)
taskoutput = subprocess.getoutput(command)
print(taskoutput)
routing.py
from django.urls import re_path
from . import consumers
websocket_urlpatterns = [
re_path(r'ws/dashboard/task/(?P<task_id>[0-9a-f-]+)', consumers.ChatConsumer),
]
网站URLhttp://localhost:8000/dashboard/task/aa3c6c12-5446-11ea-b237-1e0f691c9a55
你的 task_status
是一种方法,所以它的第一个扩充应该是 self
,python 总是在你调用函数时将这个扩充添加到函数中,这就是你出错的原因.它使用 self
和 task_id
要解决这个问题,您应该这样定义您的方法:
def task_status(self, task_id):
....
另一方面,您在 task_status
方法中同步等待,这将阻止整个服务器处理任何其他流量。
您应该将 task_status
设为 async
方法,然后使用异步子进程命令启动并 await
输出 https://docs.python.org/3/library/asyncio-subprocess.html#examples