Skip to content

Commit 7a3ffde

Browse files
author
Benjamin Moody
committed
dl_files, dl_database: avoid using multiple processes
The standard multiprocessing module is used to distribute a task to multiple processes, which is useful when doing heavy computation due to the limitations of CPython; however, making this work is dependent on the ability to fork processes or else to kludgily emulate forking on systems that don't support it. In particular, it tends to cause problems on Windows unless you are very scrupulous about how you write your program. Therefore, as a rule, the multiprocessing module shouldn't be used by general-purpose libraries, and should only be invoked by application programmers themselves (who are in a position to guarantee that imports have no side effects, the main script uses 'if __name__ == "__main__"', etc.) However, downloading a file isn't a CPU-bound task, it's an I/O-bound task, and therefore for this purpose, parallel threads should work as well or even better than parallel processes. The multiprocessing.dummy module provides the same API as the multiprocessing module, but uses threads instead of processes, so it should be safe to use in a general-purpose library.
1 parent 3b408f2 commit 7a3ffde

File tree

2 files changed

+4
-4
lines changed

2 files changed

+4
-4
lines changed

wfdb/io/download.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import multiprocessing
1+
import multiprocessing.dummy
22
import numpy as np
33
import re
44
import os
@@ -539,7 +539,7 @@ def dl_files(db, dl_dir, files, keep_subdirs=True, overwrite=False):
539539
print("Downloading files...")
540540
# Create multiple processes to download files.
541541
# Limit to 2 connections to avoid overloading the server
542-
pool = multiprocessing.Pool(processes=2)
542+
pool = multiprocessing.dummy.Pool(processes=2)
543543
pool.map(dl_pn_file, dl_inputs)
544544
print("Finished downloading files")
545545

wfdb/io/record.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import datetime
2-
import multiprocessing
2+
import multiprocessing.dummy
33
import posixpath
44
import re
55

@@ -5279,7 +5279,7 @@ def dl_database(
52795279
print("Downloading files...")
52805280
# Create multiple processes to download files.
52815281
# Limit to 2 connections to avoid overloading the server
5282-
pool = multiprocessing.Pool(processes=2)
5282+
pool = multiprocessing.dummy.Pool(processes=2)
52835283
pool.map(download.dl_pn_file, dl_inputs)
52845284
print("Finished downloading files")
52855285

0 commit comments

Comments
 (0)