久久久久久久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>

        在單獨的線程中執行 run_coroutine_threadsafe

        Executing run_coroutine_threadsafe in a separate thread(在單獨的線程中執行 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>
                  本文介紹了在單獨的線程中執行 run_coroutine_threadsafe的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  我有一個永遠運行的腳本(它檢查文件的變化).每當制作奇怪的文件時,我都需要發送 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.

                  • 問題是,事件監聽函數(下面的def run(self):)來自一個子類,所以我不能把它改成async def run(self):.因此我不能使用 await channel.send()
                  • 我對此的解決方案是使用 run_coroutine_threadsafe,如下所述:https://stackoverflow.com/一個/53726266/9283107.效果很好!但問題是,消息被放入隊列中,并且在此腳本完成之前它們永遠不會被發送(在我的情況下是:永遠不會).我假設發送消息函數被放入該腳本所在的線程中,因此線程永遠不會到達它們?
                  • 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 調用阻塞代碼,例如 time.sleep()定義.要啟動一個阻塞函數并讓它與 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 事件循環,而您的后臺線程將檢查文件,使用 asyncio.run_coroutine_threadsafe() 與 asyncio 和 discord 進行通信.

                  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 假定您有多個線程正在運行(因此是線程安全的"),其中一個運行事件循環.在您實現之前,任何使用 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.

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

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

                  相關文檔推薦

                  How to make a discord bot that gives roles in Python?(如何制作一個在 Python 中提供角色的不和諧機器人?)
                  Discord bot isn#39;t responding to commands(Discord 機器人沒有響應命令)
                  Can you Get the quot;About mequot; feature on Discord bot#39;s? (Discord.py)(你能得到“關于我嗎?Discord 機器人的功能?(不和諧.py))
                  message.channel.id Discord PY(message.channel.id Discord PY)
                  How do I host my discord.py bot on heroku?(如何在 heroku 上托管我的 discord.py 機器人?)
                  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>
                            主站蜘蛛池模板: 国产精品一区二区三区久久 | 国产精品久久久久久一区二区三区 | 精品国产乱码久久久久久88av | 欧美福利 | 一级毛片免费完整视频 | 成人3d动漫一区二区三区91 | 久久国产欧美日韩精品 | 久久不卡| 久草新在线 | 激情一区二区三区 | 国产精品成人国产乱一区 | 亚洲天天干| 一区二区视频在线观看 | 亚洲高清av在线 | 欧美国产一区二区三区 | 日韩插插 | 欧美一区不卡 | 成人免费小视频 | 欧美一区二区三区在线观看 | 特黄一级 | 黄色网址在线免费播放 | 在线视频国产一区 | 成人免费看黄网站在线观看 | 国产特级毛片 | 国产精久久久久久久妇剪断 | 国产一区三区在线 | 亚洲成人免费av | www亚洲精品 | 久久一区二区三区四区五区 | 欧美激情精品久久久久久 | 免费成人高清在线视频 | 中文字幕av亚洲精品一部二部 | 欧美视频一区 | 国产高清久久久 | 欧美久久久久久久久 | 国产精品免费在线 | 精品欧美一区免费观看α√ | 成人综合一区二区 | 成人黄色av| 久久国产婷婷国产香蕉 | 国产在线精品一区二区三区 |