Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
API design an choice
#5
One suggestion could be like this,so here wait live in a none blocking way using asyncio
import asyncio
from pathlib import Path

async def file_exists(file_path: Path) -> bool:
    """Asynchronously check if a file exists."""
    await asyncio.sleep(0.5)
    return file_path.exists()

async def wait_until_all_files_exist(file_paths: tuple[Path]):
    """Wait until all specified files exist, with periodic updates every 10 seconds."""
    while True:
        results = await asyncio.gather(*(file_exists(fp) for fp in file_paths))
        if all(results):
            print("All files found")
            break
        else:
            # List missing files
            missing_files = [fp for fp, exists in zip(file_paths, results) if not exists]
            print(f"Still missing files: {', '.join(str(mf) for mf in missing_files)}")
        await asyncio.sleep(10)

async def main():
    await wait_until_all_files_exist((Path('file1.txt'), Path('file2.txt'), Path('file3.txt')))

asyncio.run(main())
If run this will work like this,as soon as it make file2.txt and file3.txt in folder will update until all done.
G:\div_code\reader_env
λ python all_files_1.py
Still missing files: file2.txt, file3.txt
Still missing files: file2.txt, file3.txt
Still missing files: file3.txt
Still missing files: file3.txt
Still missing files: file3.txt
Still missing files: file3.txt
All files found
Reply


Messages In This Thread
API design an choice - by Skaperen - Apr-27-2024, 12:58 AM
RE: API design an choice - by Gribouillis - Apr-27-2024, 07:27 AM
RE: API design an choice - by Skaperen - Apr-27-2024, 05:26 PM
RE: API design an choice - by Gribouillis - Apr-28-2024, 06:44 AM
RE: API design an choice - by snippsat - Apr-28-2024, 04:05 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020