它是一个Web挖掘脚本.
def printer(q,missing):
while 1:
tmpurl=q.get()
try:
image=urllib2.urlopen(tmpurl).read()
except httplib.HTTPException:
missing.put(tmpurl)
continue
wf=open(tmpurl[-35:]+".jpg","wb")
wf.write(image)
wf.close()
Run Code Online (Sandbox Code Playgroud)
q是一个Queue()由Urls组成的``缺少一个空队列来收集错误提升网址
它由10个线程并行运行.
每次我跑这个,我得到了这个.
File "C:\Python27\lib\socket.py", line 351, in read
data = self._sock.recv(rbufsize)
File "C:\Python27\lib\httplib.py", line 541, in read
return self._read_chunked(amt)
File "C:\Python27\lib\httplib.py", line 592, in _read_chunked
value.append(self._safe_read(amt))
File "C:\Python27\lib\httplib.py", line 649, in _safe_read
raise IncompleteRead(''.join(s), amt)
IncompleteRead: IncompleteRead(5274 bytes read, 2918 more expected)
Run Code Online (Sandbox Code Playgroud)
但我确实使用了except......我尝试过其他类似的东西
httplib.IncompleteRead
urllib2.URLError
Run Code Online (Sandbox Code Playgroud)
甚至,
image=urllib2.urlopen(tmpurl,timeout=999999).read()
Run Code Online (Sandbox Code Playgroud)
但这都不起作用..
我怎么能抓住IncompleteRead和URLError?
我正在学习Coroutine,但它很奇怪,我无法理解......这是源头
@coroutine
def printer():
tmp=(yield)
print tmp
def sender(coru):
coru.send("hello")
print "I'm sender"
if __name__=="__main__":
coru=printer()
sender(coru)
print "I'm master"
Run Code Online (Sandbox Code Playgroud)
节目
你好
StopItertationError @ coru.send("你好")
而,
@coroutine
def printer():
while 1:
tmp=(yield)
print tmp
def sender(coru):
coru.send("hello")
print "I'm sender"
if __name__=="__main__":
coru=printer()
sender(coru)
print "I'm master"
Run Code Online (Sandbox Code Playgroud)
节目
你好
我是发件人
我是主人
正确.
所以我很想知道
为什么coroutine总是使用循环以及为什么第一个例子会出现错误
我听说3.3版本的'yield from'语法.是否有助于使第一个工作?
我知道每个协程功能都与子程序不同.
但是,我认为,在打印机功能结束后,应该终止程序而不返回发件人.
但确实如此.这不意味着发送者优于打印机吗?那么子程序和协程之间有什么区别呢.
感谢阅读,如果你让我高兴,我真的很感激:)
我正在实施greenlet API.
from greenlet import greenlet
def test1():
print 12
gr2.switch()
print 34
def test2():
print 56
gr1.switch()
print 78
gr1 = greenlet(test1)
gr2 = greenlet(test2)
gr1.switch()
Run Code Online (Sandbox Code Playgroud)
这是我凌乱的代码
def test1():
tmp1=yield
print 12
try:
gv2.send(1)
except StopIteration:
pass
tmp1=yield
print 34
def test2():
tmp2=yield
print 56
try:
gv2.send(1)
except StopIteration:
pass
tmp1=yield
print 78
gv1=test1()
gv1.next()
gv2=test2()
gv2.next()
gv1.send(1)
Run Code Online (Sandbox Code Playgroud)
可见,
12
56
Traceback (most recent call last):
File "prog.py", line 26, in <module>
gv1.send(1)
File "prog.py", line 5, in test1 …Run Code Online (Sandbox Code Playgroud)