python读取sqlserver的数据_Python:使用并发未来进程P读取sqlserver数据
我是一個從未使用過任何并行處理方法的新手。我希望從SQL Server讀取大量數據(即至少200萬行),并希望使用并行處理來加快讀取速度。下面是我使用并發未來進程池進行并行處理的嘗試。在class DatabaseWorker(object):
def __init__(self, connection_string, n, result_queue = []):
self.connection_string = connection_string
stmt = "select distinct top %s * from dbo.KrishAnalyticsAllCalls" %(n)
self.query = stmt
self.result_queue = result_queue
def reading(self,x):
return(x)
def pooling(self):
t1 = time.time()
con = pyodbc.connect(self.connection_string)
curs = con.cursor()
curs.execute(self.query)
with concurrent.futures.ProcessPoolExecutor(max_workers=8) as executor:
print("Test1")
future_to_read = {executor.submit(self.reading, row): row for row in curs.fetchall()}
print("Test2")
for future in concurrent.futures.as_completed(future_to_read):
print("Test3")
read = future_to_read[future]
try:
print("Test4")
self.result_queue.append(future.result())
except:
print("Not working")
print("\nTime take to grab this data is %s" %(time.time() - t1))
df = DatabaseWorker(r'driver={SQL Server}; server=SPROD_RPT01; database=Reporting;', 2*10**7)
df.pooling()
我當前的實現沒有得到任何輸出。"Test1"打印,就這樣。沒有其他事情發生。我理解并發未來文檔提供的各種示例,但我無法在這里實現它。我將非常感謝你的幫助。謝謝您。在
總結
以上是生活随笔為你收集整理的python读取sqlserver的数据_Python:使用并发未来进程P读取sqlserver数据的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ambari mysql error c
- 下一篇: hello python的代码,pyth