[英]How to run an async forever thread together with other threads in Python?
我正在嘗試與其他線程一起運行異步run_forever
。 但我只從永遠線程中獲得輸出,讓我認為第二個線程甚至沒有啟動。
代碼 - Classes.py:
import asyncio
class Class1:
def __init__(self):
self.loop = asyncio.get_event_loop()
async def _funcClass1(self):
while True:
await asyncio.sleep(5)
print('Hello async/threading world')
def funcClass1(self):
asyncio.ensure_future(self._funcClass1())
self.loop.run_forever()
class Class2:
def __init__(self):
self.loop = asyncio.get_event_loop()
async def _funcClass2(self, name):
print(f'Hello {name}')
def funcClass2(self):
self.loop.run_until_complete(self._funcClass2())
代碼 - main.py:
from Classes import Class1, Class2
import threading
import asyncio
class1 = Class1()
class2 = Class2()
if __name__ == "__main__":
t1 = threading.Thread(target=asyncio.run, args=(class1.funcClass1(), ))
t2 = threading.Thread(target=class2.funcClass2, args=('john', ))
t1.start()
t2.start()
t1.join()
t2.join()
輸出:
Hello async/threading world
Hello async/threading world
Hello async/threading world
Hello async/threading world
Hello async/threading world
Hello async/threading world
Hello async/threading world
Hello async/threading world
Hello async/threading world
run_forever()
將阻止下一行代碼。
在運行第二個線程后嘗試放置它
classes.py
import asyncio
class Class1:
def __init__(self):
self.loop = asyncio.get_event_loop()
async def _funcClass1(self):
while True:
await asyncio.sleep(5)
print('Hello async/threading world')
def funcClass1(self):
asyncio.ensure_future(self._funcClass1())
class Class2:
def __init__(self):
self.loop = asyncio.get_event_loop()
async def _funcClass2(self, name):
print(f'Hello {name}')
def funcClass2(self):
self.loop.run_until_complete(self._funcClass2())
main.py
from Classes import Class1, Class2
import threading
import asyncio
class1 = Class1()
class2 = Class2()
if __name__ == "__main__":
t1 = threading.Thread(target=asyncio.run, args=(class1.funcClass1(), ))
t2 = threading.Thread(target=class2.funcClass2, args=('john', ))
t1.start()
t2.start()
class1.loop.run_forever()
t1.join()
t2.join()
在玩弄了@danangjoyoo 的答案之后。 我想出了這個(兩個函數都需要作為target=asyncio.run
執行):
classes.py
:
import asyncio
class Class1:
def __init__(self):
self.loop = asyncio.get_event_loop()
async def _funcClass1(self):
while True:
await asyncio.sleep(5)
print('Hello async/threading world')
def funcClass1(self):
asyncio.ensure_future(self._funcClass1())
class Class2:
def __init__(self):
self.loop = asyncio.get_event_loop()
async def _funcClass2(self, name):
print(f'Hello {name}')
def funcClass2(self, name):
asyncio.ensure_future(self._funcClass2(name))
main.py
:
from Classes import Class1, Class2
import threading
import asyncio
class1 = Class1()
class2 = Class2()
if __name__ == "__main__":
t1 = threading.Thread(target=asyncio.run, args=(class1.funcClass1()))
t2 = threading.Thread(target=asyncio.run, args=(class2.funcClass2('john')))
class1.loop.run_forever()
class2.loop.run_until_complete()
t1.start()
t2.start()
t1.join()
t2.join()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.