Python中的线程vs多进程vs Twisted

Python中的线程vs多进程vs Twisted,python,mysql,multithreading,twisted,multiprocessing,Python,Mysql,Multithreading,Twisted,Multiprocessing,我可以得到帮助将此代码从线程转换为多进程吗。 那么谁能帮我把这段代码转换成twisted呢 使用twisted上传db会有好处吗 在Python内部与外部工具之间 import os, pyodbc, sys, threading, Queue class WorkerThread(threading.Thread): def __init__(self, queue): threading.Thread.__init__(self) self.qu

我可以得到帮助将此代码从线程转换为多进程吗。 那么谁能帮我把这段代码转换成twisted呢

使用twisted上传db会有好处吗
在Python内部与外部工具之间

import  os, pyodbc, sys, threading, Queue


class WorkerThread(threading.Thread):
    def __init__(self, queue):
        threading.Thread.__init__(self)
        self.queue = queue

    def run(self):
        while 1:
            try: # take a job from the queue
                type  = self.queue.get_nowait()

            except Queue.Empty:
                raise SystemExit

            try:
               cxn = pyodbc.connect('DSN=MySQL;PWD=MLML;Option=3') 
               csr = cxn.cursor()    
               # Inserts,update, CRUD

            except:
               # count = count +1
                print 'DB Error', type

if __name__ == '__main__':
    connections =  25

    sml = ('A', 'B', 'C','D',)
    # build a queue with tuples
    queue = Queue.Queue()

    for row in sml:
        if not row or row[0] == "#":
            continue
        queue.put(row) 

    threads = []
    for dummy in range(connections):
        t = WorkerThread(queue)
        t.start()
        threads.append(t)

    # wait for all threads to finish
    for thread in threads:
        thread.join()

    sys.stdout.flush()

#csr.close()
#cxn.close()
print 'Finish'  

更新:JP提到了和模块,我不知道。这些允许Twisted异步访问两个数据库(无需使用线程池)

请注意,如果您决定使用Twisted的enterprise adbapi,它最终将使用线程池来处理数据库连接,大致相当于您现有的示例。看


通过使用
Process
实例替换线程,并使用多处理
queue
实现,您应该能够直接转换基于线程/队列的代码以使用多处理模块。请参阅更新的

:JP提到了我不知道的和模块。这些允许Twisted异步访问两个数据库(无需使用线程池)

请注意,如果您决定使用Twisted的enterprise adbapi,它最终将使用线程池来处理数据库连接,大致相当于您现有的示例。看


通过使用
Process
实例替换线程,并使用多处理
queue
实现,您应该能够直接转换基于线程/队列的代码以使用多处理模块。请参阅。

您是否有特定的问题,或者您真的只是想让我们为您编写代码?我在阅读您的可变缩进python伪代码时遇到了问题。请你改一下缩进好吗。或者甚至发布一个工作示例?+1 nmichaels这看起来不是一个问题。您是否有特定的问题,或者您真的只是想让我们为您编写代码?我在阅读您的可变缩进python伪代码时遇到问题。请你改一下缩进好吗。或者甚至发布一个工作示例?+1 nmichaels这看起来不是问题。您可以使用txpostgres或txmysql在没有线程的情况下与postgres和mysql对话。@JP我不知道这些项目,谢谢。我更新了答案。您可以使用txpostgres或txmysql在没有线程的情况下与postgres和mysql对话。@JP我不知道这些项目,谢谢。我更新了答案。