current problems in UbuntuExpress
kamstrup
dlist at ubuntuforums.org
Wed Aug 17 02:36:32 CDT 2005
> But putting the full module's path seems ugly to me. Anyway, the
current
> method is not very nice either.
You can use relative paths with pydoc.importfile - no problems. It
works fine for me.
>> ... generic (ie. non-gtk-specific) way to do inter
>> process communication between the *-ends?
> Yes I need that.
Ok. DBUS is quite generic (and would prolly be usable), but seems
overkill to me...
>> A simpler way might be to let the backend do non-blocking writes to
a
>> named pipe and let the front-end read from that pipe end update the
>> progress bar at each read.[/color]
> May you put a example of that?
Code:
--------------------
#!/usr/bin/env python
#
# Mikkel Kamstrup Erlandsen, August 2005
#
# Pipes example - use for what ever (and how ever)
# you like.
# Demonstrates the use of pipes in a threaded
# Python environment. Processes should behave
# no different.
import os
import fcntl
import time
import random
from threading import Thread
class MessengerThread (Thread):
"""Does non-blocking writes to a pipe"""
def __init__ (self, write_fd):
Thread.__init__(self)
self.write_fd = write_fd
# Set up write_fd for non-blocking writes
flags = fcntl.fcntl (self.write_fd, fcntl.F_GETFL, 0)
flags = flags | os.O_NONBLOCK
fcntl.fcntl (self.write_fd, fcntl.F_SETFL, flags)
def run (self):
for x in "abcdefg":
time.sleep (random.random())
os.write (self.write_fd, x)
print "wrote: %s" % x
print "End of messenger"
class RecieverThread (Thread):
"""Reads stuff from a pipe and outputs them"""
def __init__ (self, read_fd):
Thread.__init__(self)
self.read_fd = read_fd
def run (self):
while 1:
letter = os.read (self.read_fd, 1)
if letter != "g": print "read: %s" % letter
else:
print "read: %s" % letter
break
print "End of reciever"
if __name__ == "__main__":
# create a pipe
read_fd, write_fd = os.pipe()
messenger = MessengerThread (write_fd)
reciever = RecieverThread (read_fd)
messenger.start ()
reciever.start ()
#wait for the threads so we can close their file descriptors
messenger.join ()
reciever.join ()
os.close (write_fd)
os.close (read_fd)
--------------------
>> I am wondering why exactly you choose processes over threads?
> People told me threads messing to much with the gtk interfaces and
is
> better to avoid when it's possible.
Well you have to be a bit careful when using threads in pygtk but it is
definitely doable. I do it my self. Search the FAQ on www.pygtk.org for
"thread" if you're into it. There's some thuth to your point though, so
it might still be the best choice anyway.
Cheers!
--
kamstrup
More information about the ubuntu-devel
mailing list