Skip to content

Commit 1671ce7

Browse files
committed
feat: better support for 3.10, use threads instead of processes
1 parent 4aca9dd commit 1671ce7

File tree

2 files changed

+11
-15
lines changed

2 files changed

+11
-15
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
```python
2828
from python_notion_exporter import NotionExporter
2929

30-
# Important to run in __main__ or in a method due to multiprocessing.
3130
if __name__ == "__main__":
3231
exporter = NotionExporter(
3332
token_v2="YOUR_NOTION_TOKEN",

src/python_notion_exporter/main.py

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
1-
import enum
1+
import concurrent
22
import json
33
import logging
44
import multiprocessing
55
import os
66
import shutil
77
import time
8+
from concurrent.futures import ThreadPoolExecutor
89
from datetime import datetime
9-
from multiprocessing import Pool, freeze_support
1010

1111
import requests
1212
from tqdm import tqdm
1313

1414

15-
class ExportType(enum.StrEnum):
15+
class ExportType():
1616
MARKDOWN = "markdown"
1717
HTML = "html"
1818
PDF = "pdf"
1919

2020

21-
class ViewExportType(enum.StrEnum):
21+
class ViewExportType():
2222
CURRENT_VIEW = "currentView"
2323
ALL = "all"
2424

@@ -77,10 +77,10 @@ def _export(self, id):
7777
url = "https://www.notion.so/api/v3/enqueueTask"
7878
id = self._to_uuid_format(s=id)
7979
export_options = {
80-
"exportType": self.export_type.value,
80+
"exportType": self.export_type,
8181
"locale": "en",
8282
"timeZone": "Europe/London",
83-
"collectionViewExportType": self.current_view_export_type.value,
83+
"collectionViewExportType": self.current_view_export_type,
8484
"flattenExportFiletree": self.flatten_export_file_tree,
8585
}
8686

@@ -184,11 +184,12 @@ def _unpack(self):
184184

185185
def process(self):
186186
logging.info(f"Exporting {len(self.pages)} pages...")
187-
with Pool(processes=self.workers) as pool:
187+
188+
with ThreadPoolExecutor(max_workers=self.workers) as executor:
188189
with tqdm(total=len(self.pages), dynamic_ncols=True) as pbar:
189-
for result in pool.imap_unordered(
190-
self._process_page, self.pages.items()
191-
):
190+
futures = {executor.submit(self._process_page, item): item for item in self.pages.items()}
191+
for future in concurrent.futures.as_completed(futures):
192+
result = future.result()
192193
if result["state"] == "failure":
193194
continue
194195
name = result["name"]
@@ -200,7 +201,3 @@ def process(self):
200201
pbar.update(1)
201202

202203
self._unpack()
203-
204-
205-
if __name__ == "__main__":
206-
freeze_support()

0 commit comments

Comments
 (0)