【发布时间】:2014-09-04 12:49:19
【问题描述】:
我正在寻找一种解决方案,可以在不等待答复的情况下发出大量异步 Web 请求。
这是我当前的代码:
import mechanize
from mechanize._opener import urlopen
from mechanize._form import ParseResponse
from multiprocessing import Pool
brow = mechanize.Browser()
brow.open('https://website.com')
#Login
brow.select_form(nr = 0)
brow.form['username'] = 'user'
brow.form['password'] = 'password'
brow.submit()
while(true):
#async open the browser until some state is fullfilled
brow.open('https://website.com/needthiswebsite')
上面代码的问题是,如果我尝试打开两个浏览器,bro2 必须等待 bro1 完成才能启动。 (它的阻塞)
bro1.open('https://website.com/needthiswebsite')
bro2.open('https://website.com/needthiswebsite')
解决方案的尝试:
#PSUDO-CODE
#GLOBAL VARIABLE STATE
boolean state = true
while(state):
#async open the browser until some state is full filled
#I spam this function until I get a positive answer from one of the calls
pool = Pool(processes = 1)
result = pool.apply_async(openWebsite,[brow1],callback = updateState)
def openWebsite(browser):
result = browser.open('https://website.com/needthiswebsite')
if result.something() == WHATIWANT:
return true
return false
def updateState(state):
state = true
我试图为我的问题实施类似的解决方案,例如: Asynchronous method call in Python?stackoverflow 上的问题。
问题是我在尝试使用 pool.apply_async(brow.open()) 时遇到错误
错误信息:
PicklingError: Can't pickle : attribute lookup builtin.function failed
我尝试了很多方法来修复 PicklingError,但似乎没有任何效果。
- 是否可以通过 mechanize 做到这一点?
- 我应该改用另一个库,比如 urllib2 或类似的东西吗?
任何帮助将不胜感激:)
【问题讨论】:
标签: python asynchronous multiprocessing mechanize