From d7a2a473d5c45ca3f67be9b4ec865f69d2c9bba8 Mon Sep 17 00:00:00 2001 From: Wim Pomp Date: Mon, 28 Dec 2020 13:52:05 +0100 Subject: [PATCH] Update readme with if __name__ == '__main__': --- README.md | 37 ++++++++++++++++++++++++++++++++++++- setup.py | 2 +- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 20d7a9f..4926b44 100644 --- a/README.md +++ b/README.md @@ -57,6 +57,7 @@ On OSX the buffer bar does not work due to limitations of the OS. ### Normal serial for loop << from time import sleep + a = 3 fun = [] for i in range(10): @@ -66,7 +67,7 @@ On OSX the buffer bar does not work due to limitations of the OS. >> [0, 3, 12, 27, 48, 75, 108, 147, 192, 243] -### Using parfor on the same loop +### Using parfor to parallelize << from time import sleep from parfor import parfor @@ -87,6 +88,40 @@ On OSX the buffer bar does not work due to limitations of the OS. >> [0, 3, 12, 27, 48, 75, 108, 147, 192, 243] +### Using parfor in a script/module/.py-file +Parfor should never be executed during the import phase of a .py-file. To prevent that from happening +use the `if __name__ == '__main__':` structure: + + << + from time import sleep + from parfor import parfor + + if __name__ == '__main__': + @parfor(range(10), (3,)) + def fun(i, a): + sleep(1) + return a*i**2 + print(fun) + + >> [0, 3, 12, 27, 48, 75, 108, 147, 192, 243] +or: + + << + from time import sleep + from parfor import parfor + + def my_fun(*args, **kwargs): + @parfor(range(10), (3,)) + def fun(i, a): + sleep(1) + return a*i**2 + return fun + + if __name__ == '__main__': + print(my_fun()) + + >> [0, 3, 12, 27, 48, 75, 108, 147, 192, 243] + ### If you hate decorators not returning a function pmap maps an iterator to a function like map does, but in parallel diff --git a/setup.py b/setup.py index 4a31e1a..fac7e58 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ with open("README.md", "r") as fh: setuptools.setup( name="parfor", - version="2020.11.24", + version="2020.12.28", author="Wim Pomp", author_email="wimpomp@gmail.com", description="A package to mimic the use of parfor as done in Matlab.",