Here's an abstraction from my code. There may be better ways to do it,
but it works.

Let's say you wanted to start a miner to watch the system log...

from subprocess import *
from multiprocessing import Process, Queue

def doSomething(self, q):
    p1 = Popen([ 'tail', '-f', '/var/log/messages'), stdout=PIPE)
    p2 = Popen(['grep', '-i', 'foo'], stdin=p1.stdout, stdout=PIPE)
    q.put(dict(pid=p1.pid))

def startDoSomething(self):
    q = Queue()
    p = Process(target=doSomething, args=(q,))
    p.start()
    db.settings.insert(pid=q.get()['pid'],
timestamp=datetime.datetime.now())
    db.commit()

The reason I did it this way is that I need to control the start/stop
of this secondary process. There will only be one instance of it
running at any given time and special permissions are needed to access
the control functions.

--Nite

(Note, this example doesn't really make sense since we aren't doing
anything with the stdout from the grep command, but illustrates the
point of how to start a secondary process which was the purpose)

On May 23, 11:26 pm, Massimo Di Pierro <[email protected]>
wrote:
> write a normal python program using multiprocessing...
>
> within each process do
>
> from gluon.shell import env
> globals().update(env('appname',import_models=True))
>
> and now you have your own db, request, response, etc.
>
> I did not try it but it should work fine.
> There may be a path issue since this expects to find applications in
> ther current working folder.
>
> Massimo
>
> On May 23, 9:47 pm, pbreit <[email protected]> wrote:
>
>
>
>
>
>
>
> > Use the multiprocessing library (to implement background processes).

Reply via email to