【发布时间】:2018-02-25 15:01:56
【问题描述】:
所以我有一个问题。我试图让我的导入更快,所以我开始使用多处理模块将一组导入分成两个函数,然后在单独的核心上运行每个函数,从而加快导入速度。但是现在代码根本无法识别模块。我做错了什么?
import multiprocessing
def core1():
import wikipedia
import subprocess
import random
return wikipedia, subprocess, random
def core2():
from urllib import request
import json
import webbrowser
return request, json, webbrowser
if __name__ == "__main__":
start_core_1 = multiprocessing.Process(name='worker 1', target=core1, args = core2())
start_core_2 = multiprocessing.Process(name='worker 2', target=core2, args = core1())
start_core_1.start()
start_core_2.start()
while True:
user = input('[!] ')
with request.urlopen('https://api.wit.ai/message?v=20160511&q=%s&access_token=Z55PIVTSSFOETKSBPWMNPE6YL6HVK4YP' % request.quote(user)) as wit_api: # call to wit.ai api
wit_api_html = wit_api.read()
wit_api_html = wit_api_html.decode()
wit_api_data = json.loads(wit_api_html)
intent = wit_api_data['entities']['Intent'][0]['value']
term = wit_api_data['entities']['search_term'][0]['value']
if intent == 'info_on':
with request.urlopen('https://kgsearch.googleapis.com/v1/entities:search?query=%s&key=AIzaSyCvgNV4G7mbnu01xai0f0k9NL2ito8vY6s&limit=1&indent=True' % term.replace(' ', '%20')) as response:
google_knowledge_base_html = response.read()
google_knowledge_base_html = google_knowledge_base_html.decode()
google_knowledge_base_data = json.loads(google_knowledge_base_html)
print(google_knowledge_base_data['itemListElement'][0]['result']['detailedDescription']['articleBody'])
else:
print('Something')
【问题讨论】:
-
您面临的确切错误/问题是什么?尝试详细描述您的问题。
-
@Reck 我收到 TypeError: can't pickle module objects 错误,您可以在下图中看到。 prntscr.com/ijkm0x
-
这应该在问题中提到/添加。以便更容易理解确切的问题。
-
通过错误消息我可以推断出来。您作为 args 传递的模块实例被馈送到多处理进程。这些进程使用 pickle 来存储进程转储。这里的问题是 pickle module 不能 pickle 模块对象。请缩进你的代码。
-
你可能想check this一次。