If I need to call some synchronous code from a coroutine, does it matter if I use a coroutine for that synchronous code?
Example: Does it matter if I use get_hello
or coro_get_hello
? And if not, what is the correct / preferred way?
import asyncio
import re
class Foo:
def get_hello(self, string):
return re.findall(r"(H.+?)\n", string)
async def coro_get_hello(self, string):
return re.findall(r"(H.+?)\n", string)
async def worker(self):
my_string = "Hello\nWorld\n" * 200
for _ in range(5):
print(await self.coro_get_hello(my_string))
print(self.get_hello(my_string))
await asyncio.sleep(1)
def start(self):
asyncio.run(self.worker())
if __name__ == "__main__":
Foo().start()```