[Tutor] threading vs. forking?

Mathias Mamsch Zabelkind@web.de
Thu Jul 10 06:35:01 2003


Threading is quite easy, once you know how it is done. I think for solving
your problem there are several advantages using a thread instead of a fork

If you fork, another process will be generated. So the complete memory of
the parent will be copied to another process - thats a waste of resources as
the child process does not need gui data. As you run in a 32 Bit enviroment
the new process will not get access to the data of the parent process. You
will have to use a shared memory or something to transfer data from the
child to the parent which is slower because you have to copy around your
data alot.
Another problem I had with forking is killing the forked process, when you
want to. A problem is for example, when the parent exits before the forked
child, you will get a ghost process.
That is all much easier with a thread. You can end, suspend and resume
threads from the parent easily. And if your parent exits suddenly the thread
will be ended automatically.

I think there are a lot of examples how threading works out there. I cannot
give you one at moment but look for example here:
http://mail.python.org/pipermail/thread-sig/2001-January/000568.html

Hope this helped a bit for your decision.

Regards, Mathias Mamsch


----- Original Message -----
From: "Thomas CLive Richards" <thomi@thomi.imail.net.nz>
To: <tutor@python.org>
Sent: Thursday, July 10, 2003 10:34 AM
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.
>
> 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...
>
> so, what are the differences between forking and threading? why would
> you use one, and not the other?
>
> thanks,
>
>
>
> --
>
> Thomi Richards,
> thomi@thomi.imail.net.nz
>
>
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor