简体   繁体   English

asyncore回调启动线程......好吗?

[英]asyncore callbacks launching threads… ok to do?

I'm unfamiliar with asyncore , and have very limited knowledge of asynchronous programming except for a few intro to twisted tutorials. 我不熟悉asyncore ,并且对异步编程的知识非常有限,除了一些扭曲的教程介绍。

I am most familiar with threads and use them in all my apps. 我最熟悉线程并在我的所有应用程序中使用它们。 One particular app uses a couchdb database as its interface. 一个特定的应用程序使用couchdb数据库作为其接口。 This involves longpolling the db looking for changes and updates. 这涉及对数据库进行longpolling以寻找更改和更新。 The module I use for couchdb is couchdbkit . 我用于couchdb的模块是couchdbkit It uses an asyncore loop to watch for these changes and send them to a callback. 它使用asyncore循环来监视这些更改并将它们发送到回调。

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. 我真的很喜欢couchdbkit,但不想在我的程序中引入问题。

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. 在仔细研究了这个之后,我还有一个关于couchdbkit大师的问题。 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. 正如您在我的代码示例中所看到的,我正在为每个线程实例化一个couchdbkit.Database对象。 I think this may be wasteful. 我认为这可能是浪费。 So, is it ok for a single database object to be used globally among the threads? 那么,在线程中全局使用单个数据库对象是否可以?

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. 我猜你最好调用服务器上的任何东西之前创建一个工作线程池,只需将一个作业添加到队列中,这些线程正在dispatch方法中读取它们的工作。

But there is no reason why mixing threading and asynchronous programming should be considered dangerous. 但是没有理由认为混合线程和异步编程应该被认为是危险的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM