<legend id='53NPy'><style id='53NPy'><dir id='53NPy'><q id='53NPy'></q></dir></style></legend>

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

        <bdo id='53NPy'></bdo><ul id='53NPy'></ul>

    1. <small id='53NPy'></small><noframes id='53NPy'>

    2. <tfoot id='53NPy'></tfoot>
      1. RuntimeError:超时上下文管理器应该在任务内部使用

        时间:2023-09-09
          <tbody id='zN5ub'></tbody>
          <legend id='zN5ub'><style id='zN5ub'><dir id='zN5ub'><q id='zN5ub'></q></dir></style></legend>

          <i id='zN5ub'><tr id='zN5ub'><dt id='zN5ub'><q id='zN5ub'><span id='zN5ub'><b id='zN5ub'><form id='zN5ub'><ins id='zN5ub'></ins><ul id='zN5ub'></ul><sub id='zN5ub'></sub></form><legend id='zN5ub'></legend><bdo id='zN5ub'><pre id='zN5ub'><center id='zN5ub'></center></pre></bdo></b><th id='zN5ub'></th></span></q></dt></tr></i><div id='zN5ub'><tfoot id='zN5ub'></tfoot><dl id='zN5ub'><fieldset id='zN5ub'></fieldset></dl></div>
              <bdo id='zN5ub'></bdo><ul id='zN5ub'></ul>
              <tfoot id='zN5ub'></tfoot>
                • <small id='zN5ub'></small><noframes id='zN5ub'>

                  本文介绍了RuntimeError:超时上下文管理器应该在任务内部使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  背景:我在一个不和谐的客户端旁边托管一个烧瓶服务器

                  Background: I am hosting a flask server alongside a discord client

                  flask 服务器只需要将消息从客户端传递给 discord,并将消息从 discord 传递给客户端.

                  The flask server just needs to pass on messages from the client to discord and from messages from discord to the client.

                  当我调用 loop.run_until_complete(sendMsg(request)) 时出现错误我在 sendMsgwait_for loop.run_until_complete()

                  I am getting the error when I call loop.run_until_complete(sendMsg(request)) I have tried wait_for in sendMsg and wait_for loop.run_until_complete()

                  我到处找了,没有发现任何东西,所以任何帮助都将不胜感激.

                  I have looked everywhere and haven't found anything so any help would be appreciated.

                  代码:

                  import discord
                  import json
                  import os
                  import asyncio
                  from flask import Flask, request, render_template
                  from async_timeout import timeout
                  from threading import Thread
                  from time import sleep
                  
                  client = discord.Client()
                  messages = []
                  app = Flask(__name__)
                  
                  def startClient():
                      loop = asyncio.new_event_loop()
                      asyncio.set_event_loop(loop)
                      client.run('token')
                  
                  
                  #
                  # Discord Events
                  #
                  @client.event
                  async def on_ready():
                      print('Discord Client Ready')
                  
                  @client.event
                  async def on_message(message):
                      global messages
                      message.append(message)
                  
                  
                  #
                  # Flask Stuff
                  #
                  async def sendMsg(request):
                      await client.send_message(discord.Object('channel id'), request.form['message'])
                  
                  
                  @app.route("/chat/", methods=['GET', 'POST'])
                  def chatPage():
                      global messages
                  
                      if request.method == 'GET':
                          return render_template('main.html')
                  
                      elif request.method == 'POST':
                          loop = asyncio.new_event_loop()
                          loop.run_until_complete(sendMsg(request))
                          return ''
                  
                  @app.route("/chat/get", methods=['GET'])
                  def chatGet():
                      return json.dumps(messages[int(request.args['lastMessageId']):])
                  
                  
                  # Start everything
                  os.environ["WERKZEUG_RUN_MAIN"] = 'true'
                  print('Starting discord.py client')
                  Thread(target=startClient).start()
                  print('Starting flask')
                  app.run(host='0.0.0.0', debug=True)
                  

                  追溯:

                  Traceback (most recent call last):
                    File "/home/SuperKooks/.local/lib/python3.5/site-packages/flask/app.py", line 2309, in __call__
                      return self.wsgi_app(environ, start_response)
                    File "/home/SuperKooks/.local/lib/python3.5/site-packages/flask/app.py", line 2295, in wsgi_app
                      response = self.handle_exception(e)
                    File "/home/SuperKooks/.local/lib/python3.5/site-packages/flask/app.py", line 1741, in handle_exception
                      reraise(exc_type, exc_value, tb)
                    File "/home/SuperKooks/.local/lib/python3.5/site-packages/flask/_compat.py", line 35, in reraise
                      raise value
                    File "/home/SuperKooks/.local/lib/python3.5/site-packages/flask/app.py", line 2292, in wsgi_app
                      response = self.full_dispatch_request()
                    File "/home/SuperKooks/.local/lib/python3.5/site-packages/flask/app.py", line 1815, in full_dispatch_request
                      rv = self.handle_user_exception(e)
                    File "/home/SuperKooks/.local/lib/python3.5/site-packages/flask/app.py", line 1718, in handle_user_exception
                      reraise(exc_type, exc_value, tb)
                    File "/home/SuperKooks/.local/lib/python3.5/site-packages/flask/_compat.py", line 35, in reraise
                      raise value
                    File "/home/SuperKooks/.local/lib/python3.5/site-packages/flask/app.py", line 1813, in full_dispatch_request
                      rv = self.dispatch_request()
                    File "/home/SuperKooks/.local/lib/python3.5/site-packages/flask/app.py", line 1799, in dispatch_request
                      return self.view_functions[rule.endpoint](**req.view_args)
                    File "/mnt/c/Users/SuperKooks/Documents/Coding/HTML/kindle-discord/app.py", line 51, in chatPage
                      loop.run_until_complete(sendMsg(request))
                    File "/usr/lib/python3.5/asyncio/base_events.py", line 387, in run_until_complete
                      return future.result()
                    File "/usr/lib/python3.5/asyncio/futures.py", line 274, in result
                      raise self._exception
                    File "/usr/lib/python3.5/asyncio/tasks.py", line 239, in _step
                      result = coro.send(None)
                    File "/mnt/c/Users/SuperKooks/Documents/Coding/HTML/kindle-discord/app.py", line 39, in sendMsg
                      await client.send_message(discord.Object('382416348007104513'), request.form['message'])
                    File "/home/SuperKooks/.local/lib/python3.5/site-packages/discord/client.py", line 1152, in send_message
                      data = yield from self.http.send_message(channel_id, content, guild_id=guild_id, tts=tts, embed=embed)
                    File "/home/SuperKooks/.local/lib/python3.5/site-packages/discord/http.py", line 137, in request
                      r = yield from self.session.request(method, url, **kwargs)
                    File "/home/SuperKooks/.local/lib/python3.5/site-packages/aiohttp/client.py", line 555, in __iter__
                      resp = yield from self._coro
                    File "/home/SuperKooks/.local/lib/python3.5/site-packages/aiohttp/client.py", line 197, in _request
                      with Timeout(timeout, loop=self._loop):
                    File "/home/SuperKooks/.local/lib/python3.5/site-packages/async_timeout/__init__.py", line 39, in __enter__
                      return self._do_enter()
                    File "/home/SuperKooks/.local/lib/python3.5/site-packages/async_timeout/__init__.py", line 76, in _do_enter
                      raise RuntimeError('Timeout context manager should be used '
                  RuntimeError: Timeout context manager should be used inside a task
                  

                  推荐答案

                  aiohttp.ClientSession() 想要在协程中被调用.尝试将您的 Client() 初始化程序移动到任何 async def 函数

                  aiohttp.ClientSession() wants to be called inside a coroutine. Try moving your Client() initializer into any async def function

                  这篇关于RuntimeError:超时上下文管理器应该在任务内部使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Discord.py 如何在另一个命令中调用另一个命令? 下一篇:不和谐.py |为某人添加角色

                  相关文章

                  最新文章

                  • <bdo id='WN1Bj'></bdo><ul id='WN1Bj'></ul>
                  <tfoot id='WN1Bj'></tfoot>

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

                  1. <legend id='WN1Bj'><style id='WN1Bj'><dir id='WN1Bj'><q id='WN1Bj'></q></dir></style></legend>
                    <i id='WN1Bj'><tr id='WN1Bj'><dt id='WN1Bj'><q id='WN1Bj'><span id='WN1Bj'><b id='WN1Bj'><form id='WN1Bj'><ins id='WN1Bj'></ins><ul id='WN1Bj'></ul><sub id='WN1Bj'></sub></form><legend id='WN1Bj'></legend><bdo id='WN1Bj'><pre id='WN1Bj'><center id='WN1Bj'></center></pre></bdo></b><th id='WN1Bj'></th></span></q></dt></tr></i><div id='WN1Bj'><tfoot id='WN1Bj'></tfoot><dl id='WN1Bj'><fieldset id='WN1Bj'></fieldset></dl></div>