473,405 Members | 2,300 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Interprocess communication and memory mapping

Oh wise readers of comp.lang.python,

Lend a newbie your ears. I have read several old articles from this
group about memory mapping and interprocess communication and have
Googled the sh** out of the internet, but have not found sufficient to
answer my questions.

Suppose that I am writing a ray tracer in Python. Well, perhaps not a
ray tracer. Suppose that I am writing a ray tracer that has to update
sixty times a second (Ignore for now that this is impossible and silly.
Ignore also that one would probably not choose Python to do such a
thing.). Ray tracing, as some of you may know, is an inherently
parallelizable task. Hence, I would love to split the task across my
quad-core CPU (Ignore also that such things do not exist yet.).
Because of GIL, I need all of my work to be done in separate processes.

My vision for this is that I would create a controller process and read
in the data (The lights, the matrices describing all of the objects,
and their colors.), putting it into a memory mapped file. Then I would
create a single child process for each CPU and assign them each a range
of pixels to work on. I would say GO and they would return the results
of their computations by placing them in an array in the memory mapped
file, which, when completed, the parent process would pump out to the
frame buffer. Meanwhile, the parent process is collecting changes from
whatever is controlling the state of the world. As soon as the picture
is finished, the parent process adjusts the data in the file to reflect
the new state of the world, and tells the child processes to go again,
etc.

So, I have a couple of questions:

* Is there any way to have Python objects (Such as a light or a color)
put themselves into a byte array and then pull themselves out of the
same array without any extra work? If each of the children had to load
all of the values from the array, we would probably lose much of the
benefit of doing things this way. What I mean to say is, can I say to
Python, "Interpret this range of bytes as a Light object, interpret
this range of bytes as a Matrix, etc." This is roughly equivalent to
simply static_casting a void * to an object type in C++.

* Are memory mapped files fast enough to do something like this? The
whole idea is that I would avoid the cost of having the whole world
loaded into memory in every single process. With threads, this is not
a problem -- what I am trying to do is figure out the Pythonic way to
work around the impossibility of using more than one processor because
of the GIL.

* Are pipes a better idea? If so, how do I avoid the problem of
wasting extra memory by having all of the children processes hold all
of the data in memory as well?

* Are there any other shared memory models that would work for this
task?

OK, I think that is enough. I look forward eagerly to your replies!

Yours,

James Aguilar

Dec 14 '05 #1
3 4776
James Aguilar wrote:
Suppose that I am writing a ray tracer in Python. Well, perhaps not a
ray tracer. Suppose that I am writing a ray tracer that has to update
sixty times a second (Ignore for now that this is impossible and silly.
Ignore also that one would probably not choose Python to do such a
thing.).
Someone doesn't agree with you there... ;-)

http://www.pawfal.org/index.php?page=PyGmy
Ray tracing, as some of you may know, is an inherently parallelizable task.
Hence, I would love to split the task across my quad-core CPU (Ignore also that
such things do not exist yet.). Because of GIL, I need all of my work to be done in
separate processes.
Right. I suppose that you could just use the existing parallel
processing mechanisms for which Python interfaces exist. However, much
has been said about making multicore parallelism more accessible to the
average thread programmer, although much of that was said on the
python-dev mailing list [1], presumably because those doing most of the
talking clearly don't think of discussing such issues with the wider
community (and probably wanted to petition for core language changes as
well).

[...]
* Is there any way to have Python objects (Such as a light or a color)
put themselves into a byte array and then pull themselves out of the
same array without any extra work?


Unless you mean something very special about "extra work", I would have
thought that the pickle module would cover this need.

[Other interesting questions about memory mapped files, pipes, shared
memory.]

My idea was to attempt to make use of existing multiprocessing
mechanisms, putting communications facilities on top. I don't know how
feasible or interesting that is, but what I wanted to do with the
pprocess module [2] was to develop an API using the POSIX fork system
call which resembled existing APIs for threading and communications. My
reasoning is that, as far as I know/remember, fork in modern POSIX
systems lets processes share read-only data - so like multithreaded
programs, each process shares the "context" of a computation with the
other computation units - whilst any modified data is held only by the
modifying process. With the supposed process migration capabilities of
certain operating systems, it should be possible to distribute
processes across CPUs and even computation nodes.

The only drawback is that one cannot, in a scheme as described above,
transparently modify global variables in order to communicate with
other processes. However, I consider it somewhat more desirable to
provide explicit communications channels for such communications, and
it is arguably a matter of taste as to how one then uses those
channels: either by explicitly manipulating channel objects, like
streams, or by wrapping them in such a way that a distributed
computation just looks like a normal function invocation.

Anyway, I don't have any formal experience in multiprocessing or any
multiprocessor/multicore environments available to me, so what I've
written may be somewhat naive, but should anything like it be workable,
it'd be a gentler path to parallelism than hacking Python's runtime to
remove the global interpreter lock.

Paul

[1]
http://mail.python.org/pipermail/pyt...er/056801.html
[2] http://www.python.org/pypi/parallel

Dec 15 '05 #2
In article <11**********************@g43g2000cwa.googlegroups .com>,
"James Aguilar" <ag***********@gmail.com> wrote:
....
So, I have a couple of questions:

* Is there any way to have Python objects (Such as a light or a color)
put themselves into a byte array and then pull themselves out of the
same array without any extra work? If each of the children had to load
all of the values from the array, we would probably lose much of the
benefit of doing things this way. What I mean to say is, can I say to
Python, "Interpret this range of bytes as a Light object, interpret
this range of bytes as a Matrix, etc." This is roughly equivalent to
simply static_casting a void * to an object type in C++.
Not exactly. One basic issue is that a significant amount of the
storage associated with a light or a color is going to be "overhead"
specific to the interpreter process image, and not shareable. A
Python process would not be able to simply acquire a lot of objects
by mapping a memory region.

However, if you're ready to go to the trouble to implement your
data types in C, then you can do the (void *) thing with their data,
and then these objects would automatically have the current value
of the data at that address. I'm not saying this is a really good
idea, but right off hand it seems technically possible. The simplest
thing might be to copy the array module and make a new type that
works just like it but borrows its storage instead of allocating it.
That would be expedient, maybe not as fast because each access to
the data comes at the expense of creating an object.
* Are memory mapped files fast enough to do something like this?
Shared memory is pretty fast.
* Are pipes a better idea? If so, how do I avoid the problem of
wasting extra memory by having all of the children processes hold all
of the data in memory as well?


Pipes might likely be a better idea, but a lot depends on the design.

Donn Cave, do**@u.washington.edu
Dec 15 '05 #3
Paul

This is pretty useful for me. Appreciate it! My whole point is not
that I actually want to do this, but that I want to make sure that
Python is powerful enough to handle this kind of thing before I really
invest myself deeply into learning and using it. I do believe that
parallel computing is in my future, one way or another, so I want to
make sure it's possible to use python to do that well and efficiently.

- James Aguilar

Dec 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: John Norman | last post by:
I'm trying to pas data back and forth between a vb app, and a c++ app. I have tried several methods: Through a dll (written in C++), doesn't work because each app has it's own instances of the...
4
by: Charles Packer | last post by:
I need to do the following simple interprocess communication (IPC) among these processes that are all on the same box: -- A daemon waits for "I'm here" announcements from multiple clients -- One...
11
by: Tamir Khason | last post by:
What is the preferred way to exchange data between processes? Example: I have 2 applications (C#) 1) WinForms (A) 2) Command Line (B) Both od them use DataLayer (class library) C A calls to...
7
by: Michael Butscher | last post by:
Hi, this is not really Python-specific but I need it for Python. I'm wanting a method for interprocess communication which is OS- independent (sockets would be the normal way to go), but which...
4
by: batista | last post by:
Hello all, I need suggestions and possibly solutions to the problem stated below: I have an application written purely in .NET ( Windows Form Application) and another application that is...
22
by: exhuma.twn | last post by:
Hi all, Supposing you have two separate processes running on the same box, what approach would you suggest to communicate between those two processes. Let me list the ones I know of: *...
0
by: Murali | last post by:
Hi Python Gurus, I am writing a GUI app (on linux) using pygtk which would launch some external applications and display their stdout and stderr inside the output window of my application...
2
by: Murali | last post by:
Hi Python Gurus, I am writing a GUI app (on linux) using pygtk which would launch some external applications and display their stdout and stderr inside the output window of my application...
3
by: madankarmukta | last post by:
Hi all, I am very new to Implementation of the Interprocess communication.I am trying to implement the concept in c++.I created the pipe using Createnamedpipe() function ,connecting to that pipe...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.