[英]Passing data between threads in Python
我有一個運行多個Web服務的Tornado Web服務器。 我需要服務A中的一個操作來調用服務B上的操作。我正在使用Suds對該服務B進行調用。此刻一切都掛起了,除非我將Suds調用放到單獨的線程中,但是現在我需要以便從該調用(線程1)中獲取數據回到主線程,這樣我就可以將其發送回原始請求的Web服務(服務A)。 一些代碼:
if input.payment_info.issuer == 'Amex':
def t1():
url = 'http://localhost:8080/PaymentService?wsdl'
client = Client(url)
result = client.service.process_amex(payment_info.issuer, payment_info.number, payment_info.sort_code)
t = Thread(target=t1)
t.start()
if result == "Success":
return result #How can I access it to return it to the main service
抱歉,如果不清楚,我知道線程是一個復雜的問題,但是由於沒有它,代碼會掛起,因此我在這里看不到其他任何選擇。
編輯:明確地說,我不使用suds來調用服務A。suds的唯一實例是用來調用服務B。
我也嘗試過使用Alex Martelli在此處的帖子(http://stackoverflow.com/a/2846697/559504)使用Queue,但是它再次掛起。
if input.payment_info.issuer == 'Amex':
def t1(q):
url = 'http://localhost:8080/PaymentService?wsdl'
client = Client(url)
result = client.service.process_amex(payment_info.issuer, payment_info.number, payment_info.sort_code)
q.put(result)
q = Queue.Queue()
t = Thread(target=t1, args=(q))
t.daemon = True
t.start()
result = q.get()
if result == "Success":
return "Success"
這是我使用的多線程tkinter
應用程序的一個片段,它使用Queue
將數據從一個線程傳遞到另一個線程。 我認為您足以將其用作模板。
def check_q(self, _):
log = self.logwidget
go = True
while go:
try:
data = queue.get_nowait()
if not data:
data = '<end>' # put visible marker in output
go = False
log.insert(END, data)
log.see(END)
except Queue.Empty:
self.after(200, self.check_q, ())
go = False
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.