Например.
Код: Выделить всё
import os
import asyncio
class AsyncContextChangeDir:
def __init__(self, newdir):
self.curdir = os.getcwd()
self.newdir = newdir
async def __aenter__(self):
os.chdir(self.newdir)
async def __aexit__(self, exc_type, exc_value, traceback):
os.chdir(self.curdir)
async def workon_mypath():
async with AsyncContextChangeDir("/tmp"):
print("working in /tmp context manager, cwd:" + os.getcwd()) # /mypath
await asyncio.sleep(100)
print("working in /tmp context manager, cwd:" + os.getcwd()) # ???
async def workon_someotherpath():
await asyncio.sleep(10)
os.chdir("/home")
print("working in other context cwd:" + os.getcwd())
loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.gather(
workon_mypath(),
workon_someotherpath()))
p>
Как лучше всего это сделать?
Подробнее здесь: https://stackoverflow.com/questions/432 ... ext-switch