ブロッキングの問題を回避するには、スレッドでIECOMオブジェクトを使用します。
これは、スレッドとIEcomオブジェクトを一緒に使用する方法を示すシンプルで強力な例です。あなたはあなたの目的のためにそれを改善することができます。
この例では、キューを使用してメインスレッドと通信するスレッドを開始します。メインスレッドでは、ユーザーはURLをキューに追加できます。IEスレッドは1つのURLを終了した後、次のURLにアクセスします。IE COMオブジェクトがスレッドで使用されているため、Coinitializeを呼び出す必要があります
from threading import Thread
from Queue import Queue
from win32com.client import Dispatch
import pythoncom
import time
class IEThread(Thread):
def __init__(self):
Thread.__init__(self)
self.queue = Queue()
def run(self):
ie = None
# as IE Com object will be used in thread, do CoInitialize
pythoncom.CoInitialize()
try:
ie = Dispatch("InternetExplorer.Application")
ie.Visible = 1
while 1:
url = self.queue.get()
print "Visiting...",url
ie.Navigate(url)
while ie.Busy:
time.sleep(0.1)
except Exception,e:
print "Error in IEThread:",e
if ie is not None:
ie.Quit()
ieThread = IEThread()
ieThread.start()
while 1:
url = raw_input("enter url to visit:")
if url == 'q':
break
ieThread.queue.put(url)