From: Pierre-Alexandre V. <ont...@gm...> - 2013-01-10 12:20:17
|
Hello, I'm facing more and more a problem with my application which is based on NetHTTPd/NetCGI. My software is based on CGI entry point (url and arguments values). Each CGI opens a memcache + postgreSQL connexion, makes his task, closes memcache + postgreSQL connexion and return the result (a string of course, most of the time JSON data). I use Memcache for several reasons, partly because of my inability to understand how to use shared memory despite the tutorial. Some CGI are becoming to long to execute (more than 30s). Usually, the result I need to return is calculated quickly (less than 15s), but I need to make lot of treatments before returning it. Thus, I'm looking for a way to return the result my client need, and continuing all tasks I need. So, I need to launch an asynchronous task. The ugly way I only see is to make my app calling itself by Http, give to the CGI, all the information it need to continue the task. But it is so ugly, I can't make it this way. Is there a way to lauch a task asynchronously ? Can I use a thread without risk ? Regards, P-A -- --------------------- https://twitter.com/#!/ontologiae/ http://linuxfr.org/users/montaigne |
From: Gerd S. <in...@ge...> - 2013-01-10 14:12:58
|
Am Donnerstag, den 10.01.2013, 13:19 +0100 schrieb Pierre-Alexandre Voye: > Hello, > > I'm facing more and more a problem with my application which is based > on NetHTTPd/NetCGI. > > > My software is based on CGI entry point (url and arguments values). > Each CGI opens a memcache + postgreSQL connexion, makes his task, > closes memcache + postgreSQL connexion and return the result (a string > of course, most of the time JSON data). > > > I use Memcache for several reasons, partly because of my inability to > understand how to use shared memory despite the tutorial. > > > > Some CGI are becoming to long to execute (more than 30s). > Usually, the result I need to return is calculated quickly (less than > 15s), but I need to make lot of treatments before returning it. > > Thus, I'm looking for a way to return the result my client need, and > continuing all tasks I need. So, I need to launch an asynchronous > task. > > > The ugly way I only see is to make my app calling itself by Http, give > to the CGI, all the information it need to continue the task. > > But it is so ugly, I can't make it this way. > > > Is there a way to lauch a task asynchronously ? > > Can I use a thread without risk ? Normally, yes, if you ensure that library functions do not "see" that there are several threads, i.e. any object is only used by one thread at a time. There are normally no mutexes controlling parallel access built-in (except where described). There are also no global variables making multi-threading impossible (and if so, there are mutexes around them). I must admit that I do not exactly understand your architectural problem. What I've got is that - You get a request via Nethttpd - You call your service routine via the CGI-style interface - that routine computes the result to return via CGI - but that routine also needs to do some side work that is not needed for the CGI return value, and could be run outside the strict HTTP request/response cycle The answer to the question how you can run such side work depends very much on how you deploy Nethttpd. Do you use the Netplex containers, or did you follow one of the simpler examples? Gerd > > Regards, > > > P-A > > > -- > --------------------- > https://twitter.com/#!/ontologiae/ > http://linuxfr.org/users/montaigne > > ------------------------------------------------------------------------------ > Master Visual Studio, SharePoint, SQL, ASP.NET, C# 2012, HTML5, CSS, > MVC, Windows 8 Apps, JavaScript and much more. Keep your skills current > with LearnDevNow - 3,200 step-by-step video tutorials by Microsoft > MVPs and experts. ON SALE this month only -- learn more at: > http://p.sf.net/sfu/learnmore_122712 > _______________________________________________ Ocamlnet-devel mailing list Oca...@li... https://lists.sourceforge.net/lists/listinfo/ocamlnet-devel -- ------------------------------------------------------------ Gerd Stolpmann, Darmstadt, Germany ge...@ge... Creator of GODI and camlcity.org. Contact details: http://www.camlcity.org/contact.html Company homepage: http://www.gerd-stolpmann.de *** Searching for new projects! Need consulting for system *** programming in Ocaml? Gerd Stolpmann can help you. ------------------------------------------------------------ |
From: Pierre-Alexandre V. <ont...@gm...> - 2013-01-10 14:18:10
|
I followed one of the simpler example (don't remember which one), in fact, my app starts with this code : let start handlers = let (option_list, cmdline_infos) = Netplex_main.args() in (* .... cut ... *) let nethttpd_factory = Nethttpd_plex.nethttpd_factory ~handlers:handlers () in Netplex_main.startup ( Netplex_mp.mp ~keep_fd_open:true () ) Netplex_log.logger_factories Netplex_workload.workload_manager_factories [ nethttpd_factory ] cmdline_infos Normally, only the threads will use the object. Because of my inability to user shared memory, I don't have any byte of global memory in this software (I use Memcache for that) So, if i start a thread and return the string result, is there a risk that the processes is killed by the master process before the thread finish his job (In theory not) ? 2013/1/10 Gerd Stolpmann <in...@ge...> > Am Donnerstag, den 10.01.2013, 13:19 +0100 schrieb Pierre-Alexandre > Voye: > > > > > Can I use a thread without risk ? > > Normally, yes, if you ensure that library functions do not "see" that > there are several threads, i.e. any object is only used by one thread at > a time. There are normally no mutexes controlling parallel access > built-in (except where described). There are also no global variables > making multi-threading impossible (and if so, there are mutexes around > them). > > I must admit that I do not exactly understand your architectural > problem. What I've got is that > > - You get a request via Nethttpd > - You call your service routine via the CGI-style interface > - that routine computes the result to return via CGI > - but that routine also needs to do some side work that is > not needed for the CGI return value, and could be run > outside the strict HTTP request/response cycle > > The answer to the question how you can run such side work depends very > much on how you deploy Nethttpd. Do you use the Netplex containers, or > did you follow one of the simpler examples? > > Gerd > > > > -- > ------------------------------------------------------------ > Gerd Stolpmann, Darmstadt, Germany ge...@ge... > Creator of GODI and camlcity.org. > Contact details: http://www.camlcity.org/contact.html > Company homepage: http://www.gerd-stolpmann.de > *** Searching for new projects! Need consulting for system > *** programming in Ocaml? Gerd Stolpmann can help you. > ------------------------------------------------------------ > > -- --------------------- https://twitter.com/#!/ontologiae/ http://linuxfr.org/users/montaigne |
From: Gerd S. <in...@ge...> - 2013-01-10 14:56:11
|
Am Donnerstag, den 10.01.2013, 15:17 +0100 schrieb Pierre-Alexandre Voye: > > > I followed one of the simpler example (don't remember which one), in > fact, my app starts with this code : > > let start handlers = > let (option_list, cmdline_infos) = Netplex_main.args() in > > (* .... cut ... *) > let nethttpd_factory = Nethttpd_plex.nethttpd_factory > ~handlers:handlers () in > Netplex_main.startup ( Netplex_mp.mp ~keep_fd_open:true () ) > Netplex_log.logger_factories > Netplex_workload.workload_manager_factories > [ nethttpd_factory ] > cmdline_infos > > > Normally, only the threads will use the object. That's fine then. > Because of my inability to user shared memory, I don't have any byte > of global memory in this software (I use Memcache for that) > > > So, if i start a thread and return the string result, is there a risk > that the processes is killed by the master process before the thread > finish his job (In theory not) ? In theory and practice, this risk exists in deed. But you can do something about it. The process is not killed with a signal, but there is an orderly shutdown procedure going through several steps until terminating the process, and it is possible to run some user code for process cleanup. Nethttpd_plex.nethttpd_factory takes a ~hooks argument: let hooks = object inherit Netplex_kit.empty_processor_hooks() method shutdown() = wait_for_the_termination_of_the_threads() end I think a simple counter for the number of threads would be sufficient (remember that "incr counter" and "decr counter" are atomic), and you could just busy-wait (while !counter > 0 do sleep 1 done). Note that there is also a second hook, pre_finish_hook, which is even called later, but this hook is time-bound. If the maximum time is exceeded, the process is finally killed (the timeout is set by ~terminate_tmo of Netplex_mp.mp). Gerd > > > 2013/1/10 Gerd Stolpmann <in...@ge...> > Am Donnerstag, den 10.01.2013, 13:19 +0100 schrieb > Pierre-Alexandre > Voye: > > > > > Can I use a thread without risk ? > > > Normally, yes, if you ensure that library functions do not > "see" that > there are several threads, i.e. any object is only used by one > thread at > a time. There are normally no mutexes controlling parallel > access > built-in (except where described). There are also no global > variables > making multi-threading impossible (and if so, there are > mutexes around > them). > > I must admit that I do not exactly understand your > architectural > problem. What I've got is that > > - You get a request via Nethttpd > - You call your service routine via the CGI-style interface > - that routine computes the result to return via CGI > - but that routine also needs to do some side work that is > not needed for the CGI return value, and could be run > outside the strict HTTP request/response cycle > > The answer to the question how you can run such side work > depends very > much on how you deploy Nethttpd. Do you use the Netplex > containers, or > did you follow one of the simpler examples? > > Gerd > > > > > -- > ------------------------------------------------------------ > Gerd Stolpmann, Darmstadt, Germany ge...@ge... > Creator of GODI and camlcity.org. > Contact details: http://www.camlcity.org/contact.html > Company homepage: http://www.gerd-stolpmann.de > *** Searching for new projects! Need consulting for system > *** programming in Ocaml? Gerd Stolpmann can help you. > ------------------------------------------------------------ > > > > > -- > --------------------- > https://twitter.com/#!/ontologiae/ > http://linuxfr.org/users/montaigne > > ------------------------------------------------------------------------------ > Master Visual Studio, SharePoint, SQL, ASP.NET, C# 2012, HTML5, CSS, > MVC, Windows 8 Apps, JavaScript and much more. Keep your skills current > with LearnDevNow - 3,200 step-by-step video tutorials by Microsoft > MVPs and experts. ON SALE this month only -- learn more at: > http://p.sf.net/sfu/learnmore_122712 > _______________________________________________ Ocamlnet-devel mailing list Oca...@li... https://lists.sourceforge.net/lists/listinfo/ocamlnet-devel -- ------------------------------------------------------------ Gerd Stolpmann, Darmstadt, Germany ge...@ge... Creator of GODI and camlcity.org. Contact details: http://www.camlcity.org/contact.html Company homepage: http://www.gerd-stolpmann.de *** Searching for new projects! Need consulting for system *** programming in Ocaml? Gerd Stolpmann can help you. ------------------------------------------------------------ |