Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Creating a python extension that works with multiprocessing.Queue

Reply
Thread Tools

Creating a python extension that works with multiprocessing.Queue

 
 
Travis Miller
Guest
Posts: n/a
 
      03-15-2009
I am very new to the python C API, and have written a simple type
called SU2 that has 4 members that are all doubles. Everything seems
to work fine (can import my module and instantiate the new type and
act on it with various methods I have defined), except for when I
attempt to use my new type with multiprocessing.Queue (by the way I am
working with python 2.6).

So when I declare my object instance and call the put() method on the
Queue instance, the object I get out with the get() method of the same
Queue instance is not the same object. The four members of the
object are all coming through as all zeros. Below is the snippet of
code.

from multiprocessing import Queue, Process
from gauge import SU2 # this is my new module and type

def func(q):
s = SU2(1.0, 2.0, 3.0, 4.0)
q.put(s)

q = Queue()
p = Process(target=func, args=(q,))
p.start()
s = q.get()
p.join()


The problem is that the s I get in the get() method has its members
set to 0.0 rather than what I set in func(). So the members are not
being put in the Queue correctly. I have defined the SU2 type using
the python C API and so I suspect I have neglected to define a method
that is needed by Queue to do its stuff. What am I missing?

Travis Miller


 
Reply With Quote
 
 
 
 
Gabriel Genellina
Guest
Posts: n/a
 
      03-18-2009
En Sun, 15 Mar 2009 01:51:35 -0200, Travis Miller <>
escribió:

> I am very new to the python C API, and have written a simple type
> called SU2 that has 4 members that are all doubles. Everything seems
> to work fine (can import my module and instantiate the new type and
> act on it with various methods I have defined), except for when I
> attempt to use my new type with multiprocessing.Queue (by the way I am
> working with python 2.6).
>
> So when I declare my object instance and call the put() method on the
> Queue instance, the object I get out with the get() method of the same
> Queue instance is not the same object. The four members of the
> object are all coming through as all zeros. Below is the snippet of
> code.


Are you on Windows? multiprocessing uses pickles to transfer objects
between processes. See if you can dump and load those kind of objects. If
not, you may need to implement __getstate__/__setstate__ or __reduce__
See http://docs.python.org/library/pickle.html


--
Gabriel Genellina

 
Reply With Quote
 
 
 
 
Travis Miller
Guest
Posts: n/a
 
      03-20-2009
I'm on linux actually. I found that so long as I implement
__reduce__, then pickling works, and my extension works with Queue
objects correctly. So far the C Api is really cool. I can do all the
math stuff where I need the speed, and still be able to use python
which is way more expressive.

travis

On Mar 17, 8:43*pm, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
> En Sun, 15 Mar 2009 01:51:35 -0200, Travis Miller <raph...@gmail.com> *
> escribió:
>
> > I am very new to the python C API, and have written a simple type
> > called SU2 that has 4 members that are all doubles. *Everything seems
> > to work fine (can import my module and instantiate the new type and
> > act on it with various methods I have defined), except for when I
> > attempt to use my new type with multiprocessing.Queue (by the way I am
> > working with python 2.6).

>
> > So when I declare my object instance and call the put() method on the
> > Queue instance, the object I get out with the get() method of the same
> > Queue instance is not the same object. * The four members of the
> > object are all coming through as all zeros. *Below is the snippet of
> > code.

>
> Are you on Windows? multiprocessing uses pickles to transfer objects *
> between processes. See if you can dump and load those kind of objects. If *
> not, you may need to implement __getstate__/__setstate__ or __reduce__
> Seehttp://docs.python.org/library/pickle.html
>
> --
> Gabriel Genellina


 
Reply With Quote
 
Gabriel Genellina
Guest
Posts: n/a
 
      03-20-2009
En Fri, 20 Mar 2009 00:16:29 -0200, Travis Miller <>
escribió:

> So far the C Api is really cool. I can do all the
> math stuff where I need the speed, and still be able to use python
> which is way more expressive.


Sure! One gets the best of both worlds that way.

--
Gabriel Genellina

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem in running a basic code in python 3.3.0 that includes HTML file Satabdi Mukherjee Python 1 04-04-2013 07:48 PM
Creating a binary only python distribution of a C extension moduleand including some additional python and c files in it akhilesh singhania Python 0 01-01-2012 12:42 PM
Creating a binary only python distribution of a C extension moduleand including some additional python and c files in it akhilesh singhania Python 0 12-30-2011 05:27 PM
When I turn on my PC, it works, works, works. Problem! Fogar Computer Information 1 01-17-2006 12:57 AM
After rebooting my PC works, works, works! Antivirus problem? Adriano Computer Information 1 12-15-2003 05:30 AM



Advertisments