By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,767 Members | 1,303 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,767 IT Pros & Developers. It's quick & easy.

queue for large objects, object references

P: n/a
Hi,

in a data acquisition program I have several threads,
one thread grabbing images and another doing some
processing.
I'd like to make my own image class containing the image
and additional information (a lot of data, some Megabytes).
Then I'd like to transfer this object to another thread.
I tried to transfer an image over a queue and it works!
(see code below)
Is the data transferred or only a reference?
How could I hold the data global and transfer only a reference
that no data has to be copied (speed, memory)?

Thanks for any answer,

Markus

**************************************
import Image
import Queue
import thread
import time

class Send:
def __init__(self, queue, filename):
self.queue = queue
self.image = Image.open(filename, 'r')

def send(self):
self.queue.put(self.image)

class Recv:
def __init__(self, queue):
self.queue = queue
thread.start_new_thread(self.MyThread, (10,))

def MyThread(self, start):
while 1:
while not self.queue.empty():
img = self.queue.get()
img.save("result.bmp")

time.sleep(0.3)
msgq = Queue.Queue(0)

sender = Send(msgq, "test.bmp")
receiver = Recv(msgq)

sender.send()
time.sleep(2)
Jul 18 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
> Is the data transferred or only a reference?

Everything in Python can be thought of as passed by reference. This
will mean different things to different people, which is why you should
look at these examples.

def funct(i): .... i += 1
.... k = 8
funct(k)
k 8

The integer doesn't get updated, because integers are immutable.

def funct(l): .... l = []
.... k = [1,2,3]
funct(k)
k [1, 2, 3]

Pointing the name 'l' to a new list object, doesn't change the object
that 'k' points to.

def funct(l): .... l[:] = []
.... k = [4,5,6]
funct(k)
k

[]

The list only gets updated when you actually modify the list pointed to
by the list named 'l'.
There are literally hundreds more examples I could go through. I'll let
you try to figure a few of those out.

- Josiah
Jul 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.