我目前正在尝试使用python多处理。我使用的库是multiprocess
(非multiprocessing
)。
我有以下代码,它创建了许多计算作业,并通过地图操作运行它:
pool = multiprocess.Pool(4)
all_responses = pool.map_async(wrapper_singlerun, range(10000))
pool.join()
pool.close()
但是,每当我运行此代码片段时,我都会收到以下错误:
pool.join()
File "/Users/davidal/miniconda3/lib/python3.6/site-packages/multiprocess/pool.py", line 509, in join
assert self._state in (CLOSE, TERMINATE)
AssertionError
你知道为什么会发生这种错误吗?我之前使用过pool.map_async
,但认为我需要pool rendez-vous
命令。否则,我的电脑会创建类似于forkbomb的东西,它会创建太多的线程(至少,这就是我认为的那样......)
感谢任何想法!
答案 0 :(得分:8)
问题是您在join
之前调用了close
。
multiprocess
似乎是missing its documentation,但据我所知,它基本上是stdlib multiprocessing
的一个分支,它预先制作了dill
pickle
} {for} multiprocessing
,因此multiprocessing
文档应与此相关。 (另外,在评论中,您说您可以使用close()
重新解决问题。)
所以,Pool.join
说:
等待工作进程退出。在使用
terminate()
之前,必须先致电join()
或close
。
join
方法是关闭队列发送方的方法,因此无法添加新任务。 close
方法是等待处理队列中的所有内容的方法。在关闭之前等待队列流失是行不通的。
但是您在join
之后而不是之前调用assert
。您close
或terminate
已join
all_responses.get()
,.wait()
已被get
或join
,因此断言失败。
所以,您可能只想切换这两个电话的顺序。
或者,也许您对map
的用途感到困惑,并认为在使用with Pool(…) as pool:
或config.middleware.insert_after(::Rack::Runtime,
'Rack::Auth::Basic',
"Staging") do |user, password|
[user, password] == [ENV['STAGING_USERNAME'], ENV['STAGING_PASSWORD']]
end
end
之前需要先调用它。如果是这样的话 - 你不需要这样做; require 'omniauth-oauth2'
module OmniAuth
module Strategies
class MyAuth < OmniAuth::Strategies::OAuth2
option :name, "MyAuth"
# Added Basic auth credentials for staging environment
option :client_options, {:site => "https://youshall:notpass@my-staging-server.info"}
uid{ raw_info['id'] }
info do
{
remote_id: raw_info['remote_id'],
name: raw_info['name'],
email: raw_info['email']
}
end
extra do
{
'raw_info' => raw_info
}
end
def raw_info
@raw_info ||= access_token.get('/api/me').parsed
end
end
end
end
将阻止,直到结果可用,之后您不需要Encoding::UndefinedConversionError: "\xC3" from ASCII-8BIT to UTF-8
- 51 non-project frames
1
File "/app/vendor/bundle/ruby/2.4.0/gems/oauth2-1.2.0/lib/oauth2/error.rb" line 30 in encode
2
File "/app/vendor/bundle/ruby/2.4.0/gems/oauth2-1.2.0/lib/oauth2/error.rb" line 30 in error_message
3
File "/app/vendor/bundle/ruby/2.4.0/gems/oauth2-1.2.0/lib/oauth2/error.rb" line 17 in initialize
4
File "/app/vendor/bundle/ruby/2.4.0/gems/oauth2-1.2.0/lib/oauth2/client.rb" line 112 in new
5
File "/app/vendor/bundle/ruby/2.4.0/gems/oauth2-1.2.0/lib/oauth2/client.rb" line 112 in request
6
File "/app/vendor/bundle/ruby/2.4.0/gems/oauth2-1.2.0/lib/oauth2/client.rb" line 138 in get_token
7
File "/app/vendor/bundle/ruby/2.4.0/gems/oauth2-1.2.0/lib/oauth2/strategy/auth_code.rb" line 29 in get_token
8
File "/app/vendor/bundle/ruby/2.4.0/gems/omniauth-oauth2-1.4.0/lib/omniauth/strategies/oauth2.rb" line 89 in build_access_token
9
File "/app/vendor/bundle/ruby/2.4.0/gems/omniauth-oauth2-1.4.0/lib/omniauth/strategies/oauth2.rb" line 73 in callback_phase
10
File "/app/vendor/bundle/ruby/2.4.0/gems/omniauth-1.3.1/lib/omniauth/strategy.rb" line 227 in callback_call
11
File "/app/vendor/bundle/ruby/2.4.0/gems/omniauth-1.3.1/lib/omniauth/strategy.rb" line 184 in call!
12
File "/app/vendor/bundle/ruby/2.4.0/gems/omniauth-1.3.1/lib/omniauth/strategy.rb" line 164 in call
13
File "/app/vendor/bundle/ruby/2.4.0/gems/omniauth-1.3.1/lib/omniauth/builder.rb" line 63 in call
。这实际上更常见,特别是对于{{1}}和朋友(尽管文档中的示例通过{{1}}执行,而不是手动调用池中的任何内容)。