[Tutor] threading vs. forking?

Rodrigues op73418@mail.telepac.pt
Thu Jul 10 07:34:01 2003


> -----Original Message-----
> From: tutor-admin@python.org
> [mailto:tutor-admin@python.org]On Behalf Of
> Thomas CLive Richards
> Sent: quinta-feira, 10 de Julho de 2003 9:34
> To: tutor@python.org
> Subject: [Tutor] threading vs. forking?
>
>
>
> Hi,
>
> I'm *trying* to develop a program which (amongst other things) reads
> information from a serial port and prints it in a nice GUI
> interface.
>
> The problem i have is that i don't know when data is going
> to be sent to
> the port, so i have to use a polling method to fetch the data. The
> problem with that is that while the program is polling the
> serial port,
> the GUI isn't getting updated.
>
> So, AFAIK, there are two methods around this. there are
> probably many
> more ways to do this, but i haven't a clue what they are.
>
> The first would be forking the process using os.fork(), and
> running the
> serial port polling in one fork, and the GUI in another, and somehow
> send the data to the GUI.
>

You can use a pipe to have the polling process pass whatever it has to
the gui process.

> the other would be using threading. The problem with that
> is that I've
> never done it before, and from reading the documentation, I
> can't see
> how to get information from one thread to another. There is
> likely to be
> a lot of information on the port, so the information
> passing would have
> to be easy...
>

The best way I know is to use the Queue module along with the
high-level threading module. It implements a Queue that's thread-safe.
So you could do it like this: have the gui thread (the main thread?) a
public attribute referencing the queue. The polling thread just pushes
whatever data it has to this queue, which then is popped off by the
gui thread and processed accordingly

> so, what are the differences between forking and threading?
> why would
> you use one, and not the other?
>

This is a tough question. Threads are easier when it comes to
communication since global memory is shared between threads. But this
also poses problems of it's own.

I'm leaving this one to the more knowledgeable people in the list. I'm
eager to hear their responses.

> thanks,
>
> Thomi Richards,
> thomi@thomi.imail.net.nz

With my best regards,
G. Rodrigues