Update readme with if __name__ == '__main__':

This commit is contained in:
Wim Pomp
2020-12-28 13:52:05 +01:00
parent a02677dc1d
commit d7a2a473d5
2 changed files with 37 additions and 2 deletions

View File

@@ -57,6 +57,7 @@ On OSX the buffer bar does not work due to limitations of the OS.
### Normal serial for loop ### Normal serial for loop
<< <<
from time import sleep from time import sleep
a = 3 a = 3
fun = [] fun = []
for i in range(10): 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] >> [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 time import sleep
from parfor import parfor 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] >> [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 ### If you hate decorators not returning a function
pmap maps an iterator to a function like map does, but in parallel pmap maps an iterator to a function like map does, but in parallel

View File

@@ -5,7 +5,7 @@ with open("README.md", "r") as fh:
setuptools.setup( setuptools.setup(
name="parfor", name="parfor",
version="2020.11.24", version="2020.12.28",
author="Wim Pomp", author="Wim Pomp",
author_email="wimpomp@gmail.com", author_email="wimpomp@gmail.com",
description="A package to mimic the use of parfor as done in Matlab.", description="A package to mimic the use of parfor as done in Matlab.",