Skip to content

Commit 2c253c7

Browse files
Use ProcessPoolExecutor to speed up link extraction
1 parent 9254b27 commit 2c253c7

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

linkextr.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#!/usr/bin/env python3
22

3+
from functools import reduce
34
from urllib.parse import urlparse
45

56
from mistletoe import Document
@@ -8,7 +9,9 @@
89
from mistletoe.utils import traverse
910

1011
import argparse
12+
import concurrent.futures
1113
import glob
14+
import operator
1215
import os.path
1316
import sys
1417

@@ -74,18 +77,20 @@ def main(args):
7477
if len(path) == 1 and os.path.isdir(path[0]):
7578
path = glob.iglob(os.path.join(path[0], "**", "*.md"), recursive=True)
7679

77-
links = set()
7880
args = args.__dict__
7981
if not path:
8082
lines = sys.stdin.readlines()
8183
_, lines = frontmatter_split(lines)
8284
links = findlinks(lines, **args)
8385
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())
8994

9095
result = [line + "\n" for line in sorted(links)]
9196
if output:

0 commit comments

Comments
 (0)