变量传递给同一个函数的多个线程 [英] Variable passed to multiple threads of the same function
问题描述
我目前正在开发一个使用 dhooks 将 webhooks 发送到多个 Discord 的系统.
I am currently working on a system that sends webhooks to multiple Discord using dhooks.
这是用于调用发送 webhook 的函数的函数.webhooks
的长度是 2,它是一个列表列表.
This is the function used to call the function which sends the webhooks. The length of webhooks
is 2 and it's a list of lists.
for hook in webhooks:
thread.start_new_thread(send_hook,(hook, embed, tag))
这是 send_hook 函数:
This is the send_hook function:
def send_hook(hook, embed, tag):
embed.set_footer(text="{} x Will".format(hook[0])) <-- This is the part where the error happens
webhook = Webhook(hook[1])
webhook.send(embed=embed)
我得到的错误是,当我在 send_hook
的第 2 行设置页脚时,插入到 embed 变量中的变量有时会被发送到错误的 webhooks - 几乎就像它被覆盖一样.
The error I am getting is, when I set the footer in line 2 of send_hook
, the variable inserted into the embed variable is sometimes sent to the wrong webhooks - almost as if its being over ridden.
举个例子:
这是我的 webhook 列表:[["Webhook 1 text", "discordwebhook1"], ["Webhook 2 text", "discordwebhook1"]]]
.
This is my list webhooks: [["Webhook 1 text", "discordwebhook1"], ["Webhook 2 text", "discordwebhook1"]]
.
将会发生的情况是,在discordwebhook1
的频道中,页脚会显示Webhook 1 text",但在discordwebhoo2
的频道中,页脚会显示Webhook 1"文本"也是如此.
What will happen is that in the channel of discordwebhook1
the footer will say "Webhook 1 text", but in the channel of discordwebhoo2
the footer will say "Webhook 1 text" as-well.
我尝试在 send_hook
函数中创建嵌入的新变量 - 但是这也不起作用(代码如下).
I have tried creating a new variable of the embed in the send_hook
function - however this also didn't work (code below).
def send_hook(hook, embed, tag):
new_embed = embed
new_embed.set_footer(text="{} x Will".format(hook[0])) <-- This is the part where the error happens
webhook = Webhook(hook[1])
webhook.send(embed=new_embed)
感谢所有帮助!
谢谢
推荐答案
如何获得嵌入"?请注意,'embed' 和 'tag' 总是传递给每个新创建的线程,如有必要,您需要对每个线程进行深拷贝
How do you get 'embed'? Notice that 'embed' and 'tag' are always passed to every newly created thread, you need to do a deepcopy of each if necessary
from copy import deepcopy
for hook in webhooks:
thread.start_new_thread(send_hook,(hook, deepcopy(embed), tag))
这篇关于变量传递给同一个函数的多个线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!