Python asynchronous processing in existing loop -


i'm creating module openerp in have launch ongoing process.

openerp runs in continuous loop. process has launched when click on button, , has keep running without holding openerp's execution.

to simplify it, have code:

#!/usr/bin/python import multiprocessing import time  def f(name):     while true:         try:             print 'hello', name             time.sleep(1)         except keyboardinterrupt:             return  if __name__ == "__main__":     count = 0     while true:         count += 1         print "pass %d" % count         pool = multiprocessing.pool(1)         result = pool.apply_async(f, args=['bob'])         try:             result.get()         except keyboardinterrupt:             #pass             print 'interrupted'         time.sleep(1) 

when executed, pass 1 printed once , endless series of hello bob printed until ctrl+c pressed. pass 2 obtained , on, shown below:

pass 1 hello bob hello bob hello bob ^cinterrupted pass 2 hello bob hello bob hello bob hello bob 

i passes keep increasing in parallel hello bob's.

how do that?

here can id can create multi threaded implementation of python under server memory, run independently server execution thread. trick behind used fork 1 thread server on required click , assign server variable separate copy new thread thread execute independently , @ end of process have commit transaction process not main server process. here small example of how can .

import pprint import pooler threading import thread import datetime import logging pp = pprint.prettyprinter(indent=4)  class mythread(thread):     """     """     def __init__(self, obj, cr, uid, context=none):         thread.__init__(self)         self.external_id_field = 'id'         self.obj = obj         self.cr = cr         self.uid = uid         self.context = context or {}         self.logger = logging.getlogger(module_name)         self.initialize()      """         abstract method implemented in real instance     """     def initialize(self):         """             init before import             login         """         pass      def init_run(self):         """             call after intialize run in thread, not in main process             use long initialization operation         """         pass      def run(self):         """             entry point launch process(thread)         """         try:             self.init_run()             #your code goes here             #todo add business logic             self.cr.commit()         except exception, err:             sh = stringio.stringio()             traceback.print_exc(file=sh)             error = sh.getvalue()             print error         self.cr.close() 

like can add code in module (import_base module in 6.1 or trunk) next can can make extended implementation of , make instace of service or can directly start forking treads following code:

    service = myservcie(self, cr, uid, context)     service.start() 

now start background services run faster , give freedom use ui.

hope thank you


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -