现有循环Python的异步处理 [英] Python asynchronous processing in existing loop
问题描述
我创建一个模块的OpenERP在我所发起一个持续的过程。
I'm creating a module for OpenERP in which I have to launch an ongoing process.
的OpenERP运行在连续循环。我的过程中有当我点击一个按钮来启动,它必须保持运行没有举起的OpenERP的执行。
OpenERP runs in a continuous loop. My process has to be launched when I click on a button, and it has to keep running without holding up OpenERP's execution.
要简化它,我有这样的code:
To simplify it, I have this 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)
在执行通1
打印一次,然后无限系列你好鲍勃
打印,直到 CTRL + C
是pssed $ p $。然后第二遍
获得等,如下图所示:
When executed, Pass 1
is printed once and then an endless series of hello bob
is printed until CTRL+C
is pressed. Then Pass 2
is obtained and so on, as shown below:
Pass 1
hello bob
hello bob
hello bob
^CInterrupted
Pass 2
hello bob
hello bob
hello bob
hello bob
我想传球保持平行增加你好的。
I would like the passes to keep increasing in parallel with the hello bob
's.
我该怎么办呢?
推荐答案
在这里你可以做什么ID,您可以创建那么服务器的内存,这将独立运行,然后服务器执行线程下的Python的多线程实现。
这背后的诀窍是使用将是我们餐桌上你所需的点击来自服务器的一个线程,我们将所有的服务器变量的单独副本分配给新的线程,这样的线程将单独执行,并在随后结束的过程,你必须提交事务的这个过程将不主服务器进程。在这里,它的小例子,你如何能做到这一点。
Here what you can do id you can create then Multi Threaded Implementation of Python under the server memory, which will run independently then server execution thread. Trick behind this will be used is we will fork one thread from server on your required click and we will assign all server variable separate copy to the new Thread so that thread will execute independently and at then end of process you have to commit the transaction as this process will be not main server process. Here the small example of it how you can do it .
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或主干import_base模块)的像一些模块添加一些code
现在什么接下来你可以做的是,你可以扩展实现这一点,然后让服务的instace或者您也可以直接启动分叉履带像下面code:
LIke this you can add some code in some module like (import_base module in 6.1 or trunk) Now what Next you can do is you can make extended implementation of this and then make instace of the service or you can directly start forking the treads like following code:
service = myServcie(self, cr, uid, context)
service.start()
现在这一点,我们开始其运行速度更快,让你自由地使用UI后台服务。
now this we start background services which run faster and give you freedom to use the UI.
希望这会帮助你
谢谢
Hope this will help you Thank You
这篇关于现有循环Python的异步处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!