Python Forum
Thread Rating:
  • 2 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
thread pool
#1
I want to use thread pool to speed up the process. Though there is no error in compilation, 'entering calc' is not displayed. Is my code correct?
Moreover, how to clear the screen of the Shell. I tried the methods searched from the webs but they don't work.
Furthermore, I found the followings. Why?
print x, args, kwargs // error
print(x, args, kwargs) // correct
Thank you very much.

[code]import numpy as np
from multiprocessing import Pool, TimeoutError
import time

def rand():
    return np.random.normal(0, 1, 1)

class myClass:
    def __init__( self, a = 6, b = 6.5):
        self.a = a
        self.b = b
        self.pool = Pool(processes=4)

    def calc(self, i, j):
        print('entering calc')
        return self.a**i + self.b**j

    def compute(self, n):
        result = [self.pool.apply_async(self.calc, (rand(), rand())) for k in range(n)]
        return result

 A = myClass()
 r = A.compute(10)[/code]
Reply
#2
In python V2 print is a language statement (no parentheses necessary, even if Python ignores them when given since otherwise it would create a tuple and print it) and in V3 print(...) is a plain function call.

As to your problem, not an expert but experimentation shows that:
  • The thing to execute must tbe a plain function, not an object method (maybe try a closure, if you want constant parameters?)
  • Function isn't executed until you try to retrieve the results
from multiprocessing import Pool, TimeoutError
import time,random
 
def calc():
    print('entering global calc')
    return 3

 
class myClass:
    def __init__( self, a = 6, b = 6.5):
        self.a = a
        self.b = b
        self.pool = Pool(processes=4)
 
    def calc(self, i, j):
        print('entering class calc')
        return 4
 
    def compute(self, n):
        print ('in compute()')
        result = [self.pool.apply_async(calc, ()) for k in range(n)]
        print ([res.get(timeout=1) for res in result])
        return result
 
A = myClass()
r = A.compute(10)
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#3
Thanks for your reply.

I tired to run your code but only ''in compute()' was shown and the text 'entering global calc' was not displayed.

How to make it work?
Reply
#4
Strange:
[Image: vh9fUe4.png]

What do you get if your print the contents of result? A bunch of <multiprocessing.pool.ApplyResult?
Output:
[<multiprocessing.pool.ApplyResult object at 0x7f7e8d7205c0>, <multiprocessing.pool.ApplyResult object at 0x7f7e8d720668>, <multiprocessing.pool.ApplyResult object at 0x7f7e8d720710>, <multiprocessing.pool.ApplyResult object at 0x7f7e8d7207b8>, <multiprocessing.pool.ApplyResult object at 0x7f7e8d720860>, <multiprocessing.pool.ApplyResult object at 0x7f7e8d720908>, <multiprocessing.pool.ApplyResult object at 0x7f7e8d7209b0>, <multiprocessing.pool.ApplyResult object at 0x7f7e8d720a58>, <multiprocessing.pool.ApplyResult object at 0x7f7e8d720b00>, <multiprocessing.pool.ApplyResult object at 0x7f7e8d720ba8>]
(normally followed by the entering global calc lines)
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#5
I ran the code and got:

Traceback (most recent call last):
File "C:\Users\frankie\AppData\Local\Programs\Python\Python36-32\test3.py", line 28, in <module>
r = A.compute(10)
File "C:\Users\frankie\AppData\Local\Programs\Python\Python36-32\test3.py", line 22, in compute
print ([res.get(timeout=1) for res in result])
File "C:\Users\frankie\AppData\Local\Programs\Python\Python36-32\test3.py", line 22, in <listcomp>
print ([res.get(timeout=1) for res in result])
File "C:\Users\frankie\AppData\Local\Programs\Python\Python36-32\lib\multiprocessing\pool.py", line 604, in get
raise TimeoutError
multiprocessing.context.TimeoutError

After I added if '__name__ == __main__': before 'A = myClass()', then I got the code being successfully compiled and the result was:

in compute()
[3, 3, 3, 3, 3, 3, 3, 3, 3, 3]
Reply
#6
(May-16-2017, 01:49 PM)Frankie Wrote: I ran the code and got:

Traceback (most recent call last):
 File "C:\Users\frankie\AppData\Local\Programs\Python\Python36-32\test3.py", line 28, in <module>
   r = A.compute(10)
 File "C:\Users\frankie\AppData\Local\Programs\Python\Python36-32\test3.py", line 22, in compute
   print ([res.get(timeout=1) for res in result])
 File "C:\Users\frankie\AppData\Local\Programs\Python\Python36-32\test3.py", line 22, in <listcomp>
   print ([res.get(timeout=1) for res in result])
 File "C:\Users\frankie\AppData\Local\Programs\Python\Python36-32\lib\multiprocessing\pool.py", line 604, in get
   raise TimeoutError
multiprocessing.context.TimeoutError

After I added if '__name__ == __main__': before 'A = myClass()', then I got the code being successfully compiled and the result was:

in compute()
[3, 3, 3, 3, 3, 3, 3, 3, 3, 3]

Do you run your code from an IDE or just as a shell script? I can't reproduce your results, but I runn the Python from a plain shell.
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020