File tree Expand file tree Collapse file tree 2 files changed +31
-6
lines changed Expand file tree Collapse file tree 2 files changed +31
-6
lines changed Original file line number Diff line number Diff line change @@ -20,8 +20,9 @@ the cache mechanism of pure function call with the same parameters. By adding
20
20
parameter "* debug=True* ", it is easy to show the evaluation process step by step.
21
21
22
22
>>> from lazy import Lazy
23
+ >>> lazy_int = Lazy(int, debug=True)
23
24
>>> def fib(n):
24
- ... return Lazy(int, debug=True) (n) if n <= 1 else fib(n-1) + fib(n-2)
25
+ ... return lazy_int (n) if n <= 1 else fib(n-1) + fib(n-2)
25
26
...
26
27
>>> a = fib(10)
27
28
>>> a
@@ -84,7 +85,7 @@ example shows how the implicit parallel works by using decorator *lazy*.
84
85
s.eval()
85
86
print(s)
86
87
87
- $ time python3 1 .py
88
+ $ time python3 test_parallel .py
88
89
((f1()*f2())+(f3()*f4()))
89
90
Enter f1
90
91
Enter f2
@@ -98,5 +99,26 @@ example shows how the implicit parallel works by using decorator *lazy*.
98
99
99
100
real 0m5.173s
100
101
user 0m0.111s
101
- sys 0m0.049s
102
+ sys 0m0.049s
103
+
104
+ You can easily change the number of CPU cores in parallel computing by adding
105
+ * Lazy.set_cores(num)* . You can completely disable parallel by * Lazy.set_cores(0)* .
106
+
107
+ $ time python3 test_parallel_with_zero_cores.py
108
+ ((f1()*f2())+(f3()*f4()))
109
+ Enter f2
110
+ Leave f2
111
+ Enter f1
112
+ Leave f1
113
+ Enter f3
114
+ Leave f3
115
+ Enter f4
116
+ Leave f4
117
+ 14
118
+
119
+ real 0m20.085s
120
+ user 0m0.055s
121
+ sys 0m0.016s
122
+
123
+
102
124
Original file line number Diff line number Diff line change @@ -133,13 +133,16 @@ def startjob(self):
133
133
Lazy .JOBS .discard (self )
134
134
if self .remote :
135
135
Lazy .ACTIVE += 1
136
- self .POOL .apply_async (self .operator , args , callback = self .finish )
136
+ if Lazy .POOL :
137
+ Lazy .POOL .apply_async (self .operator , args , callback = self .finish )
138
+ else :
139
+ self .finish (self .operator (* args ))
137
140
else :
138
141
self .finish (self .operator (* args ))
139
142
def eval (self ):
140
143
if self .value is not None :
141
144
return self .value
142
- if Lazy .POOL is None :
145
+ if Lazy .POOL is None and Lazy . CORES > 0 :
143
146
Lazy .POOL = Pool (Lazy .CORES )
144
147
self .remote = False
145
148
self .setdepth (0 )
@@ -152,7 +155,7 @@ def eval(self):
152
155
def schedule (cls ):
153
156
while cls .JOBS :
154
157
next = max (list (cls .JOBS ), key = lambda x :x .depth )
155
- if next .depth == - 1 or next .remote and cls .ACTIVE >= cls .CORES :
158
+ if next .depth == - 1 or next .remote and cls .CORES > 0 and cls . ACTIVE >= cls .CORES :
156
159
break
157
160
next .startjob ()
158
161
You can’t perform that action at this time.
0 commit comments