Thanks for the answer - I was aware that I don't have to do this to
handle ajax requests in general. The application I'm building needs to
send and receive a sequence of messages from the client in a specific
order, so I thought it would be easier to handle it in one thread/
process. It is however probably better to do it statelessly as you
say, so I'll probably have to rewrite the code somewhat.

Out of curiosity though, what is the right way to start a subprocess
in web2py?

On 19 Mai, 22:00, Yarko Tymciurak <[email protected]> wrote:
> On May 19, 2:14 pm, amoygard <[email protected]> wrote:
>
> > Hi,
>
> > I'm pretty new to web2py and web application frameworks. I'm trying to
> > create a new background process in controller to handle incoming ajax
> > data from a user.
>
> You are trying to do too much:  remember:  the web is "stateless" ---
> when _anything_ comes in from a client that gets directed to your URL,
> (this is just an example flow, so you can get the general idea):
>
> - somewhere, a DNS server replaces the URI  name with a destination
> address;
> - the destination server (let's say apache)  tries to sort out, and
> direct to the correct app (in this case, web2py);
> - web2py's main() parses the path part of the URI to decide how to
> route internally
> - if it finds a matching app/controller, main() will set up the
> execution environment to prepare the call:
>   - i.e.,  your models are "executed" so that the table definitions,
> etc. are all in the environment, and then your controller file
>   -  the function in your controller is called with this execution
> environment (this is the thread that you are trying to make, and
> should not bother)
>
> The controller function does it's stuff with the request, and returns
> (to main())  the response - sometimes nothing, sometimes a dict. of
> stuff;
> Main takes the return values from the controller, and processes
> appropriately, most often getting the view, parsing the template,
> creating the resulting view, and sending it back to the client.
>
> When an ajax call is made, you can see an example 
> athttp://www.web2py.com/book/default/section/10/3,  and explanation of
> what this does.
>
> The general point:   you call a controller with a request (even in the
> case of an ajax call);
> The response goes back to the  caller.
>
> Lots of stuff happens for you - you don't need to write the entire web
> server underpinnings;  just worry about the logic you want to
> implement so server and client can converse / exchange information.
>
> Hope this helps.
>
> Regards,
> - Yarko
>
>
>
> > I'm using the module multiprocessing for this.
> > However, when I start the new process, a new instance of web2py server
> > is started? I'm assuming this has something to do with forking (which
> > I don't know much about). Is there an easy workaround? Or should I
> > preferrably do this some other way, for instance with threads?
>
> > Code is as follows (controller calls function 'someinit' in a module
> > in module-folder):
>
> > def tekstpr(mstring,conn):
> >   print mstring
> >   conn.send("hello")
>
> > def someinit(userid):
> >   from multiprocessing import Pipe, Process
> >   parent_conn, child_conn = Pipe()
> >   p = Process(target=tekstpr,args=("I'm alive!",child_conn))
> >   session.procpipe = parent_conn
> >   p.start()
> >   session.procpipe.poll(300)
> >   return session.procpipe.recv()

Reply via email to