【发布时间】:2018-09-04 10:32:20
【问题描述】:
我写了一个示例代码:
import requests
from bs4 import BeautifulSoup
from threading import Thread
def test():
r = requests.get('http://zhuanlan.sina.com.cn/')
soup = BeautifulSoup(r.content,'lxml')
print('run test on main thread')
test()
print('run test on child thread')
t = Thread(target=test)
t.start()
t.join()
输出是:
run test on main thread
run test on child thread
encoding error : input conversion failed due to input error, bytes 0x95 0x50 0x22 0x20
encoding error : input conversion failed due to input error, bytes 0x95 0x50 0x22 0x20
encoding error : input conversion failed due to input error, bytes 0x95 0x50 0x22 0x20
我写了一个测试函数,在主线程和子线程中运行。如输出所示,测试函数在子线程 print encoding error: input conversion failed due to input error 中运行,我无法阻止它。为什么会这样?
【问题讨论】:
标签: python multithreading beautifulsoup thread-safety lxml