|
1 | 1 | #!/usr/bin/env python3 |
2 | 2 |
|
| 3 | +from functools import reduce |
3 | 4 | from urllib.parse import urlparse |
4 | 5 |
|
5 | 6 | from mistletoe import Document |
|
8 | 9 | from mistletoe.utils import traverse |
9 | 10 |
|
10 | 11 | import argparse |
| 12 | +import concurrent.futures |
11 | 13 | import glob |
| 14 | +import operator |
12 | 15 | import os.path |
13 | 16 | import sys |
14 | 17 |
|
@@ -74,18 +77,20 @@ def main(args): |
74 | 77 | if len(path) == 1 and os.path.isdir(path[0]): |
75 | 78 | path = glob.iglob(os.path.join(path[0], "**", "*.md"), recursive=True) |
76 | 79 |
|
77 | | - links = set() |
78 | 80 | args = args.__dict__ |
79 | 81 | if not path: |
80 | 82 | lines = sys.stdin.readlines() |
81 | 83 | _, lines = frontmatter_split(lines) |
82 | 84 | links = findlinks(lines, **args) |
83 | 85 | else: |
84 | | - for file in path: |
85 | | - with open(file, "r", encoding="utf-8") as md: |
86 | | - lines = md.readlines() |
87 | | - _, lines = frontmatter_split(lines) |
88 | | - links |= findlinks(lines, **args) |
| 86 | + with concurrent.futures.ProcessPoolExecutor(4) as executor: |
| 87 | + futures = [] |
| 88 | + for file in path: |
| 89 | + with open(file, "r", encoding="utf-8") as md: |
| 90 | + lines = md.readlines() |
| 91 | + _, lines = frontmatter_split(lines) |
| 92 | + futures.append(executor.submit(findlinks, lines, **args)) |
| 93 | + links = reduce(operator.or_, [future.result() for future in futures], set()) |
89 | 94 |
|
90 | 95 | result = [line + "\n" for line in sorted(links)] |
91 | 96 | if output: |
|
0 commit comments