同时运行2个函数

时间:2021-04-05 02:31:49

标签: discord rpc

这对我来说很奇怪。

以下使用 pypresence 库的函数 rpc_presencehttps://qwertyquerty.github.io/pypresence/html/index.html 使用 asyncio。

这是我的代码:

def stuff():
    print("do stuff")
def rpc_presence():
    while True:  # The presence will stay on as long as the program is running
        RPC.update(details="Great", state=random.choice(quotes), large_image="actual_logo")
        time.sleep(1) 

def main_func():
  rpc_thread = thread.Threading(target=rpc_presence)
  rpc_thread.start()
  stuff_thread = thread.Threading(target=rpc_presence)
  stuff_thread.start()

我收到错误:

Exception in thread Thread-3:
Traceback (most recent call last):
  File "C:\Users\Zylly\AppData\Local\Programs\Python\Python39\lib\threading.py", line 954, in _bootstrap_inner
    self.run()
  File "C:\Users\Zylly\AppData\Local\Programs\Python\Python39\lib\threading.py", line 892, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Users\Zylly\Desktop\Sypherus\Sypherus.py", line 29, in rpc_presence_thread
    RPC = Presence(client_id,pipe=0)
  File "C:\Users\Zylly\AppData\Local\Programs\Python\Python39\lib\site-packages\pypresence\presence.py", line 13, in __init__
    super().__init__(*args, **kwargs)
  File "C:\Users\Zylly\AppData\Local\Programs\Python\Python39\lib\site-packages\pypresence\baseclient.py", line 40, in __init__
    self.update_event_loop(self.get_event_loop())
  File "C:\Users\Zylly\AppData\Local\Programs\Python\Python39\lib\site-packages\pypresence\baseclient.py", line 83, in get_event_loop
    loop = asyncio.get_event_loop()
  File "C:\Users\Zylly\AppData\Local\Programs\Python\Python39\lib\asyncio\events.py", line 642, in get_event_loop
    raise RuntimeError('There is no current event loop in thread %r.'
RuntimeError: There is no current event loop in thread 'Thread-3'.

我与开发人员交谈过,他说他既不是异步开发人员,也不是线程开发人员,因此他无法真正帮助我。 我怎样才能同时运行这个函数和另一个函数,而另一个函数已经使用了 asyncio。 随意查看 pypresence 库的源代码,此外,我建议查看 baseclient.py,尤其是在 get_event_loop() 函数中。

0 个答案:

没有答案
相关问题