久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

      <i id='csQHf'><tr id='csQHf'><dt id='csQHf'><q id='csQHf'><span id='csQHf'><b id='csQHf'><form id='csQHf'><ins id='csQHf'></ins><ul id='csQHf'></ul><sub id='csQHf'></sub></form><legend id='csQHf'></legend><bdo id='csQHf'><pre id='csQHf'><center id='csQHf'></center></pre></bdo></b><th id='csQHf'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='csQHf'><tfoot id='csQHf'></tfoot><dl id='csQHf'><fieldset id='csQHf'></fieldset></dl></div>

      1. <tfoot id='csQHf'></tfoot>

        <small id='csQHf'></small><noframes id='csQHf'>

      2. <legend id='csQHf'><style id='csQHf'><dir id='csQHf'><q id='csQHf'></q></dir></style></legend>
          <bdo id='csQHf'></bdo><ul id='csQHf'></ul>

        在單獨的線程中執(zhí)行 run_coroutine_threadsafe

        Executing run_coroutine_threadsafe in a separate thread(在單獨的線程中執(zhí)行 run_coroutine_threadsafe)

            1. <small id='9pNmS'></small><noframes id='9pNmS'>

            2. <legend id='9pNmS'><style id='9pNmS'><dir id='9pNmS'><q id='9pNmS'></q></dir></style></legend>
                <bdo id='9pNmS'></bdo><ul id='9pNmS'></ul>
              • <tfoot id='9pNmS'></tfoot>

                  <i id='9pNmS'><tr id='9pNmS'><dt id='9pNmS'><q id='9pNmS'><span id='9pNmS'><b id='9pNmS'><form id='9pNmS'><ins id='9pNmS'></ins><ul id='9pNmS'></ul><sub id='9pNmS'></sub></form><legend id='9pNmS'></legend><bdo id='9pNmS'><pre id='9pNmS'><center id='9pNmS'></center></pre></bdo></b><th id='9pNmS'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='9pNmS'><tfoot id='9pNmS'></tfoot><dl id='9pNmS'><fieldset id='9pNmS'></fieldset></dl></div>
                    <tbody id='9pNmS'></tbody>
                  本文介紹了在單獨的線程中執(zhí)行 run_coroutine_threadsafe的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

                  問題描述

                  我有一個永遠(yuǎn)運行的腳本(它檢查文件的變化).每當(dāng)制作奇怪的文件時,我都需要發(fā)送 Discord 消息.

                  I have a script that is constantly running forever (it checks changes in files). I need to send Discord messages whenever a weird file is made.

                  • 問題是,事件監(jiān)聽函數(shù)(下面的def run(self):)來自一個子類,所以我不能把它改成async def run(self):.因此我不能使用 await channel.send()
                  • 我對此的解決方案是使用 run_coroutine_threadsafe,如下所述:https://stackoverflow.com/一個/53726266/9283107.效果很好!但問題是,消息被放入隊列中,并且在此腳本完成之前它們永遠(yuǎn)不會被發(fā)送(在我的情況下是:永遠(yuǎn)不會).我假設(shè)發(fā)送消息函數(shù)被放入該腳本所在的線程中,因此線程永遠(yuǎn)不會到達(dá)它們?
                  • Problem is, the event watching function (def run(self): below) is from a subclass, so I can't change it to async def run(self):. Therefore I can't use await channel.send()
                  • My solution to this was to use run_coroutine_threadsafe like explained here: https://stackoverflow.com/a/53726266/9283107. That works good! But the problem is, the messages get put into a queue and they never get sent until this script finishes (which in my case would be: never). I assume the send message functions get put into the thread that this script is on, therefore the thread never gets to them?

                  也許我們可以將 run_coroutine_threadsafe 扔到一個單獨的線程中或什么的?這是我能做的最小的例子,它仍然顯示了我的子類問題.

                  Maybe we can throw the run_coroutine_threadsafe into a separate thread or something? This is the most minimal example I can make that still shows my subclass problem.

                  import discord
                  import os
                  import asyncio
                  import time
                  
                  # CHANNEL_ID = 7659170174????????
                  client = discord.Client()
                  channel = None
                  
                  class Example():
                      # Imagine this run comes from a subclass, so you can't add sync to it!
                      def run(self):
                          # await channel.send('Test') # We can't do this because of the above comment
                          asyncio.run_coroutine_threadsafe(channel.send('Test'), _loop)
                          print('Message sent')
                  
                  @client.event
                  async def on_ready():
                      print('Discord ready')
                      global channel
                      channel = client.get_channel(CHANNEL_ID)
                  
                      for i in range(2):
                          Example().run()
                          time.sleep(3)
                  
                      print('Discord messages should appear by now. Sleeping for 20s to give it time (technically this would be infinite)')
                      time.sleep(20)
                      print('Script done. Now they only get sent for some reason')
                  
                  _loop = asyncio.get_event_loop()
                  
                  client.run('Your secret token')
                  

                  推薦答案

                  首先,請注意,您不能從 async 調(diào)用阻塞代碼,例如 time.sleep()定義.要啟動一個阻塞函數(shù)并讓它與 asyncio 通信,您可以從 on_ready 甚至從頂層生成一個后臺線程,如下所示:

                  First, note that you're not allowed to call blocking code such as time.sleep() from an async def. To start a blocking function and have it communicate with asyncio, you can spawn a background thread from on_ready or even from top-level, like this:

                  # checker_function is the function that blocks and that
                  # will invoke Example.run() in a loop.
                  threading.Thread(
                      target=checker_function,
                      args=(asyncio.get_event_loop(), channel)
                  ).start()
                  

                  您的主線程將運行 asyncio 事件循環(huán),而您的后臺線程將檢查文件,使用 asyncio.run_coroutine_threadsafe() 與 asyncio 和 discord 進(jìn)行通信.

                  Your main thread will run the asyncio event loop and your background thread will check the files, using asyncio.run_coroutine_threadsafe() to communicate with asyncio and discord.

                  正如您鏈接到的答案下的評論中指出的那樣,asyncio.run_coroutine_threadsafe 假定您有多個線程正在運行(因此是線程安全的"),其中一個運行事件循環(huán).在您實現(xiàn)之前,任何使用 asyncio.run_coroutine_threadsafe 的嘗試都會失敗.

                  As pointed out in a comment under the answer you linked to, asyncio.run_coroutine_threadsafe assumes that you have multiple threads running (hence "thread-safe"), one of which runs the event loop. Until you implement that, any attempt to use asyncio.run_coroutine_threadsafe will fail.

                  這篇關(guān)于在單獨的線程中執(zhí)行 run_coroutine_threadsafe的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

                  【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

                  相關(guān)文檔推薦

                  How to make a discord bot that gives roles in Python?(如何制作一個在 Python 中提供角色的不和諧機(jī)器人?)
                  Discord bot isn#39;t responding to commands(Discord 機(jī)器人沒有響應(yīng)命令)
                  Can you Get the quot;About mequot; feature on Discord bot#39;s? (Discord.py)(你能得到“關(guān)于我嗎?Discord 機(jī)器人的功能?(不和諧.py))
                  message.channel.id Discord PY(message.channel.id Discord PY)
                  How do I host my discord.py bot on heroku?(如何在 heroku 上托管我的 discord.py 機(jī)器人?)
                  discord.py - Automaticaly Change an Role Color(discord.py - 自動更改角色顏色)

                      <tfoot id='vuUpK'></tfoot>

                      <small id='vuUpK'></small><noframes id='vuUpK'>

                          <tbody id='vuUpK'></tbody>

                          <i id='vuUpK'><tr id='vuUpK'><dt id='vuUpK'><q id='vuUpK'><span id='vuUpK'><b id='vuUpK'><form id='vuUpK'><ins id='vuUpK'></ins><ul id='vuUpK'></ul><sub id='vuUpK'></sub></form><legend id='vuUpK'></legend><bdo id='vuUpK'><pre id='vuUpK'><center id='vuUpK'></center></pre></bdo></b><th id='vuUpK'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='vuUpK'><tfoot id='vuUpK'></tfoot><dl id='vuUpK'><fieldset id='vuUpK'></fieldset></dl></div>
                          • <bdo id='vuUpK'></bdo><ul id='vuUpK'></ul>
                            <legend id='vuUpK'><style id='vuUpK'><dir id='vuUpK'><q id='vuUpK'></q></dir></style></legend>
                            主站蜘蛛池模板: 天天干天天谢 | 天天干b| 日韩www| 国产精品a久久久久 | 一区二区精品 | 亚洲国产成人精品女人久久久 | 欧美不卡在线 | 久久久久久久久久久久久久久久久久久久 | 日韩免费av一区二区 | 久久亚洲精品国产精品紫薇 | 国产精品一区二区无线 | 亚洲成人动漫在线观看 | 中文字幕中文字幕 | 亚洲国产一区二区视频 | 91麻豆精品国产91久久久久久久久 | h视频免费在线观看 | 九九色综合 | 精品久久久久久久久久 | 中国毛片免费 | 欧美日产国产成人免费图片 | 国产日韩91 | 成人午夜精品 | 日日碰狠狠躁久久躁96avv | 99久久精品视频免费 | 亚洲一区二区三区四区在线观看 | 国产成人99久久亚洲综合精品 | 日韩和的一区二区 | 日本中文字幕在线观看 | 人人干人人爽 | 九九爱这里只有精品 | 午夜欧美一区二区三区在线播放 | 在线看一区二区 | 国产精品日日做人人爱 | 成人一区精品 | 日本成人三级电影 | 中文字幕电影在线观看 | 最新国产精品 | 中文字幕精品视频 | 午夜av电影院 | 免费久久久久久 | 午夜精品久久久久久久久久久久久 |