proc.py
import asyncio
from argparse import ArgumentParser
from asyncio import sleep
async def process_function(user, file_path):
if user == 'user1':
await sleep(2)
result = f"Process for user {user}"
with open(file_path, 'w') as fh:
fh.write(result)
print("Hello world")
if __name__ == '__main__':
parser = ArgumentParser()
parser.add_argument('--name')
parser.add_argument('--path')
args = parser.parse_args()
user = args.name
file_path = args.path
asyncio.run(process_function(user, file_path)
=====================================================
main.py
import asyncio
import subprocess
async def proc(user):
file_path = f"{user}_output.txt"
process = subprocess.Popen(['python', 'proc.py', '--name', user, '--path', file_path])
await asyncio.to_thread(process.wait)
with open(f"{user}_output.txt", "r") as output_file:
result = output_file.read()
print(result)
async def broker():
users = ['user1', 'user2', 'user3']
await asyncio.gather(*(proc(user) for user in users))
def main():
asyncio.run(broker())
if __name__ == "__main__":
main()
댓글 없음:
댓글 쓰기