asyncore 回调启动线程...可以吗?
我不熟悉 asyncore,并且对异步编程的了解非常有限,除了一些扭曲教程简介。
我最熟悉线程并在我的所有应用程序中使用它们。一个特定的应用程序使用 couchdb 数据库作为其界面。这涉及对数据库进行长轮询以查找更改和更新。我用于 couchdb 的模块是 couchdbkit。它使用 asyncore 循环来监视这些更改并将它们发送到回调。
所以,我从这个回调中计算出我启动工作线程的地方。将异步和线程编程混合起来似乎有点粗糙。我真的很喜欢 couchdbkit,但不想在我的程序中引入问题。
所以,我的问题是,从异步回调触发线程是否安全?
这是一些代码...
def dispatch(change):
global jobs, db_url # jobs is my queue
db = Database(db_url)
work_order = db.get(change['id']) # change is an id to the document that changed.
# i need to get the actual document (workorder)
worker = Worker(work_order, db) # fire the thread
jobs.append(worker)
worker.start()
return
main()
.
.
.
consumer.wait(cb=dispatch, since=update_seq, timeout=10000) #wait constains the asyncloop.
更新:
在仔细研究了这一点之后,我对 couchdbkit 专家还有一个额外的问题。可能有数百个线程使用数据库。正如您在我的代码示例中看到的,我正在为每个线程实例化一个 couchdbkit.Database 对象。我认为这可能是浪费。那么,单个数据库对象可以在线程之间全局使用吗?
I'm unfamiliar with asyncore, and have very limited knowledge of asynchronous programming except for a few intro to twisted tutorials.
I am most familiar with threads and use them in all my apps. One particular app uses a couchdb database as its interface. This involves longpolling the db looking for changes and updates. The module I use for couchdb is couchdbkit. It uses an asyncore loop to watch for these changes and send them to a callback.
So, I figure from this callback is where I launch my worker threads. It seems a bit crude to mix asynchronous and threaded programming. I really like couchdbkit, but would rather not introduce issues into my program.
So, my question is, is it safe to fire threads from an async callback?
Here's some code...
def dispatch(change):
global jobs, db_url # jobs is my queue
db = Database(db_url)
work_order = db.get(change['id']) # change is an id to the document that changed.
# i need to get the actual document (workorder)
worker = Worker(work_order, db) # fire the thread
jobs.append(worker)
worker.start()
return
main()
.
.
.
consumer.wait(cb=dispatch, since=update_seq, timeout=10000) #wait constains the asyncloop.
Update:
After looking over this more, I have an additional question for the couchdbkit gurus. There will potentially be hundreds of threads using the database. As you can see in my code sample, I am instantiating a couchdbkit.Database object per thread. I think this may be wasteful. So, is it ok for a single database object to be used globally among the threads?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这不是会在服务器每次返回新文档时创建一个新线程吗?我猜想您最好在调用服务器上的任何内容之前创建一个工作线程池,然后将一个作业添加到这些线程正在从调度中读取其工作的队列中方法。
但没有理由认为混合线程和异步编程是危险的。
Wouldn't this create a new thread every time the server returns a new document? I would guess that you are best creating a pool of worker threads before you call anything on the server, and just add a job to the queue that these threads are reading their work from in the
dispatch
method.But there is no reason why mixing threading and asynchronous programming should be considered dangerous.