Note that there are some explanatory texts on larger screens.

plurals
  1. POasyncore callbacks launching threads... ok to do?
    primarykey
    data
    text
    <p>I'm unfamiliar with <a href="http://docs.python.org/library/asyncore.html" rel="nofollow">asyncore</a>, and have very limited knowledge of asynchronous programming except for a few intro to twisted tutorials.</p> <p>I am most familiar with threads and use them in all my apps. One particular app uses a <a href="http://www.couchone.com/" rel="nofollow">couchdb</a> database as its interface. This involves longpolling the db looking for changes and updates. The module I use for couchdb is <a href="http://couchdbkit.org/" rel="nofollow">couchdbkit</a>. It uses an asyncore loop to watch for these changes and send them to a callback. </p> <p>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.</p> <p>So, my question is, is it safe to fire threads from an async callback?</p> <p>Here's some code...</p> <pre><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. </code></pre> <p>Update:</p> <p>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? </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload