Asynchronous 现有循环中的Python异步处理

Asynchronous 现有循环中的Python异步处理,asynchronous,python-2.6,openerp,Asynchronous,Python 2.6,Openerp,我正在为OpenERP创建一个模块,在这个模块中我必须启动一个正在进行的过程 OpenERP在一个连续的循环中运行。当我点击一个按钮时,我的进程必须启动,并且它必须在不妨碍OpenERP执行的情况下保持运行 为了简化,我有以下代码: #!/usr/bin/python import multiprocessing import time def f(name): while True: try: print 'hello', name

我正在为OpenERP创建一个模块,在这个模块中我必须启动一个正在进行的过程

OpenERP在一个连续的循环中运行。当我点击一个按钮时,我的进程必须启动,并且它必须在不妨碍OpenERP执行的情况下保持运行

为了简化,我有以下代码:

#!/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)
执行时,
pass1
打印一次,然后打印一系列没完没了的
hello bob
,直到按下
CTRL+C
。然后获得
pass2
,依此类推,如下所示:

Pass 1
hello bob
hello bob
hello bob
^CInterrupted
Pass 2
hello bob
hello bob
hello bob
hello bob
我希望通行证数量能与“你好,鲍勃”的通行证数量保持同步增长


如何做到这一点?

在这里,您可以在服务器内存下创建Python的多线程实现,然后在服务器执行线程下独立运行。 这背后的诀窍是,我们将在您所需的单击时从服务器分叉一个线程,并将所有服务器变量的单独副本分配给新线程,以便线程将独立执行,然后在进程结束时,您必须提交事务,因为此进程将不是主服务器进程。这里是一个小例子,说明你是如何做到这一点的

import pprint
import pooler
from 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 to be implemented in the real instance
    """
    def initialize(self):
        """
            init before import
            usually for the login
        """
        pass

    def init_run(self):
        """
            call after intialize run in the thread, not in the main process
            TO use for long initialization operation
        """
        pass

    def run(self):
        """
            this is the Entry point to launch the 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()
这样,您可以在某些模块中添加一些代码,如(导入6.1或trunk中的基本模块) 现在,您接下来可以做的是,您可以对此进行扩展实现,然后安装服务,或者您可以直接开始分叉,如以下代码所示:

    service = myServcie(self, cr, uid, context)
    service.start()
现在我们启动后台服务,它运行得更快,让您可以自由使用UI

希望这对你有帮助
谢谢你

谢谢你,果酱!这是答案的一部分;实际上,只继承线程。线程在OpenERP中不起作用。我还必须从netsvc.Server继承,然后它才能工作。然而,当我试图在DB上执行任何事务时,我遇到了错误,我不得不再次创建游标。所以我放弃了这种方法,而是创建了一个调度程序。这并不是我想要的那么快,但它做得很好。更正:这是正确的解决方案!我不得不修改我的代码,但它终于起作用了!