Re: Threads vs Processes
- Posted by Mario Steele <eumario at trilake.net> May 02, 2005
- 506 views
Bruce Douglas wrote: > > Hello, I am new at Euphoria and am really happy with it. I have 2 > projects in C++ which were stalled which I have ported to Euphoria > and have made a lot of progress now. I am using wxEuphoria > which is important for me because I work in Linux but deploy these > programs on Windows computers. So, thank-you, Rob and Matt! > > I have been following your debate on threads vs. processes with > great interest. I hope this is not a dumb question. > > My question is how you implement processes? For example, I'm working > on an email checker / deleter and would like to be able to connect > simultaneously to several accounts. Also, I have times when the connection > freezes but doesn't disconnect and then my poor program gets trapped in the > socket call and never returns. I have a timer() call set up but the > program is > not available to check the timer. > > It would be nice if I could just call a procedure but not wait for it to > finish. > When it was good and ready, it could generate an event or set a variable or > whatever. If it doesn't return in a certain time, I can have the program > handle > this more gracefully. > > I would like to avoid using the Windows API calls since I would like my > programs to still work in Linux. > > Is there a way to implement this without threads? If I am not mistaken, your creating your wxSocketClient, with the wxSOCKET_WAITALL flag implaced. I would suggest that you use the wxSOCKET_NOWAIT flag. This will allow you to receive Asynchronous data, without blocking the GUI. This ofcourse means, that you must detect, and buffer data, so that way you don't get any corruption in streamed data. It is often suggestable to use builtin features of the library, before breaking down, and using other methods in which to get your intention across the program. Something like this would be satisfactory:
sequence buff procedure parseInfo() -- do your error detection here -- If there is more data to be retrived, -- then wait for it to be retrived, -- otherwise, if you have all the data -- do your parsing of it, saving, or whatever -- and pass it along to what ever needs it. end procedure procedure on_socket_email(atom this, atom event_type, atom id, atom event) seqence dat event_type = get_socket_event(event) if event = wxSOCKET_INPUT then dat = socket_read( socket_from_event(event) ) if not equal({},dat) then buff &= dat end if if not equal({},buff) then parseInfo() end if end if end procedure set_event_handler(your_form, client_id, wxEVT_SOCKET, routine_id("main_socket_event"))
This should allow you to parse all the information, without having to rely on using sub-proccesses in order to stop the Blocking calls when you do a socket_read() event. Mario Steele http://enchantedblade.trilake.net Attaining World Dominiation, one byte at a time...