python - How to write the map sentence here to call array to calculate with ThreadPool module? -


I want to practice with module Threadpool, so that each element can add a category (1, 1,100).

Multiprocessing.pool import ThreadPool array = range (1,100) class test (): def myadd (self, x): return (x + 2) do = ThreadPool (5) do.map (test.myadd, array ) Tracebacks (Lastest Call Last): File "& lt; stdin>", Line 1, & lt; Module & gt; The file "D: \ Python34 \ lib \ multiprocessing \ pool.py", line 255, back to the map itself. _map_async (funk, iterable, mapstar, cholexise) .get () file "D: \ Python34 \ lib \ multiprocessing \ pool .py", line 594, self-lifting ._value TypeError: Exception should be obtained by basexception & gt; & Gt; & Gt; Do.map (test.myadd (array), array) traceback (most recent call final): file "& lt; stdin>", line 1, & lt; Module & gt; Name error: the name 'self' is not defined & gt; & Gt; & Gt; Do.map (test.myadd (), array) traceback (most recent call final): File "& lt; stdin>", line 1, & lt; Module & gt; TypeError: myadd () 2 Expected positional arguments are missing: 'self' and 'x'

How to write a calculation of the map, so that the array can be called to calculate? It is easy for me to do this with functions like: multiprocessing.pool import ThreadPool array = range (1,100) def myadd (x): return (x + 2) do = ThreadPool

  (5 ) Do.map (myadd, array)  

This works fine for me, when I change the function to a class in the law, then I am confused.

If you type myadd to test class An example method is going to be created, you actually call test class to myadd :

  to multiprocessing.pool import ThreadPool Square test (): def myadd (self, x): return (x + 2) t = threadpool (5) test_obj = test () # this you get from `test` class t.map (test_obj.my_add, range (1,100)) Gives an example of # Now you can call `myadd` on your example  

Comments

Popular posts from this blog

sqlite3 - UPDATE a table from the SELECT of another one -

c# - Showing a SelectedItem's Property -

javascript - Render HTML after each iteration in loop -