noslenj123 has asked for the wisdom of the Perl Monks concerning the following question:

I'm needing to write a program that can run 1, 2 or both socket servers and display their output in a list box in real time.

Ah, a perfect time for me to learn Tk! So I put together this nice interface with the top half for server1 and the bottom have for server2. Each has a button to start and stop the server and each a list box for output.

Then I started to implement the servers as Threads and then the trouble began with various kinds of crashes. A SuperSearch provided some info here that threads may not get along with Tk.

I tried launching a new thread in a sub that was called when clicking a button. That seemed okay but the thread tries to log a message to the listbox and that appears to be a problem even when I declare the sub as "sub name : locked {}".

So I'm looking for other strategies for acomplishing my task. Threads, no threads, it doesn't matter. It has to run on Win2k or WinXp. I'm using perl 5.8.7. I need to be able to start and stop the servers at any time.

Oh wise Monks, can you show me a path?

  • Comment on How to run multiple server processes under Tk?

Replies are listed 'Best First'.
Re: How to run multiple server processes under Tk?
by thundergnat (Deacon) on Sep 20, 2005 at 00:59 UTC

    In general, your information is correct, threads (and forking) do not play well with Perl/Tk. (It can be done but it isn't easy.)

    Perhaps you may be able to use the POE framework to handle the sockets? POE will work with Tk, though you need to change how you think about the event loop a bit. (at least I did.)

Re: How to run multiple server processes under Tk?
by graff (Chancellor) on Sep 20, 2005 at 01:24 UTC
    I hope someone with interest/skills in Windows will respond at some point, but in the meantime, maybe you could try setting up your socket server process as a stand-alone app, and run two instances of it like this:
    my $sspid1 = open( $sockserver1, "sock_server.pl -params |" ) or warn "can't start sock_server.pl: $!";
    and likewise for the second process (and maybe you even want the file handles and pids to be in arrays).

    This assumes that your sock_server.pl is designed so that: it gets all the info it needs for start-up (port number, etc) from command-line params; it uses its own internal file handles for reading/writing on the socket; and it writes whatever reports you want to its own STDOUT.

    As indicated in the other reply, you'll need to set up the appropriate mechanism in Tk for reading from these processes while the GUI is handling input events from the user; I think Tk::FileEvent might do for that.

    As for controlling the processes themselves (starting and stopping), maybe there's a way on Windows to send "suspend" and "resume" signals, or if it's not a big deal, just close those file handles (killing the processes) and reopen (restart) them as needed.

Re: How to run multiple server processes under Tk?
by zentara (Cardinal) on Sep 20, 2005 at 12:01 UTC
    Threads will work fine with Tk, AS LONG AS YOU CREATE THE THREADS BEFORE YOU CREATE THE MAIN Tk WINDOW.

    So you will not be able to "launch a new thread with a button click", however, you can signal an existing thread to do something. Create 2 sleeping threads first, then start a Tk front-end to control them. Check out Tk-with-worker-threads for an example of how it can be done.

    The shared hashes and sleeping thread setup is the basic model I use with Tk, and works fine on linux and windows( earlier version of Windows like 95, 98, Me; don't handle big shared hashes very well. But this example works.)

    The thing to remember with Tk and shared variables in threads, is that Tk will not "know" what the shared data is, until it actively tries to read it. So you can't use a shared variable as a reference, and expect it to update automatically....you need a timer to repeatedly check it.


    I'm not really a human, but I play one on earth. flash japh
      Sweet,

      I'm going to give that a try, it sounds promising. I'll post back my results....

        Great. I was thinking about it a little more, and thought about the problem of reading the socket in the thread, and passing the data back thru shared variables. I've yet to see a way in main, to read a filehandle (socket) in the thread. So you might have to work out a scheme of pushing your socket reads into a shared array in the thread, then in the Tk program, setup a timer to shift data off of the shared array. That is the way I would do it, but you may have luck finding a way to read filehandles cross-thread.

        I'm not really a human, but I play one on earth. flash japh
Re: How to run multiple server processes under Tk?
by noslenj123 (Scribe) on Sep 20, 2005 at 04:30 UTC
    Thanks guys! I'll take a look at POE and give the other suggestion a try. But it sounds like I will have to abandon as I need something pretty quickly and it's turning out to be much easier in vb.net with threads, and I'm just learning it.

    I always try to do it in perl first because it's usually way easier and of course, I love perl.

    If anyone else has any thoughts, I'd love to hear them.