Created
August 2, 2019 14:34
-
-
Save SebDeclercq/1ebf2a076aebbc5eaa08e0adcaa7a860 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pathlib import Path | |
from typing import List | |
import asyncio | |
import time | |
class DirScanner: | |
'''Directory scanner''' | |
async def scan(self, dirname: str) -> List[Path]: | |
'''Scan recursively a directory and yield Path instance | |
for every file within''' | |
files: List[Path] = [] | |
for file in Path(dirname).iterdir(): | |
if file.is_file(): | |
files.append(file) | |
elif file.is_dir(): | |
files.extend(*await asyncio.gather(*[self.scan(str(file))])) | |
return files | |
async def cb(path: str) -> None: | |
scan: DirScanner = DirScanner() | |
for _ in await scan.scan(path): | |
_ | |
s = time.time() | |
asyncio.run(cb('../..')) | |
print(f'{time.time() - s} second(s)') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pathlib import Path | |
from typing import AsyncIterator | |
import asyncio | |
import time | |
class DirScanner: | |
'''Directory scanner''' | |
async def scan(self, dirname: str) -> AsyncIterator[Path]: | |
'''Scan recursively a directory and yield Path instance | |
for every file within''' | |
for file in Path(dirname).iterdir(): | |
if file.is_file(): | |
yield file | |
elif file.is_dir(): | |
async for file in self.scan(str(file)): | |
yield file | |
async def cb(path: str) -> None: | |
scan: DirScanner = DirScanner() | |
async for _ in scan.scan(path): | |
_ | |
s = time.time() | |
asyncio.run(cb('../..')) | |
print(f'{time.time() - s} second(s)') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pathlib import Path | |
from typing import List | |
import time | |
class DirScanner: | |
'''Directory scanner''' | |
def scan(self, dirname: str) -> List[Path]: | |
'''Scan recursively a directory and yield Path instance | |
for every file within''' | |
files: List[Path] = [] | |
for file in Path(dirname).iterdir(): | |
if file.is_file(): | |
files.append(file) | |
elif file.is_dir(): | |
files.extend(self.scan(str(file))) | |
return files | |
def cb(path: str) -> None: | |
scan: DirScanner = DirScanner() | |
for _ in scan.scan(path): | |
_ | |
s = time.time() | |
cb('../..') | |
print(f'{time.time() - s} second(s)') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pathlib import Path | |
from typing import Iterator | |
import time | |
class DirScanner: | |
'''Directory scanner''' | |
def scan(self, dirname: str) -> Iterator[Path]: | |
'''Scan recursively a directory and yield Path instance | |
for every file within''' | |
for file in Path(dirname).iterdir(): | |
if file.is_file(): | |
yield file | |
elif file.is_dir(): | |
for file in self.scan(str(file)): | |
yield file | |
def cb(path: str) -> None: | |
scan: DirScanner = DirScanner() | |
for _ in scan.scan(path): | |
_ | |
s = time.time() | |
cb('../..') | |
print(f'{time.time() - s} second(s)') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PS C:\Users\sdq\Desktop\DEV\tmp> python sync.py | |
2.7073585987091064 second(s) | |
PS C:\Users\sdq\Desktop\DEV\tmp> python async.py | |
3.3078598976135254 second(s) | |
PS C:\Users\sdq\Desktop\DEV\tmp> python sync_gen.py | |
2.663944721221924 second(s) | |
PS C:\Users\sdq\Desktop\DEV\tmp> python async_gen.py | |
2.752870559692383 second(s) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment