我目前正在使用火炬来训练多线程深度学习网,我需要注册一个信号处理程序,这样当我按Docker Quickstart Terminal.app
时我可以保存网络的权重。但问题是,我发现多线程程序只是忽略了信号,我完全不明白为什么......
见下面的代码
ctrl+c
和
-- threadTester.lua
local classic = require 'classic'
local threads = require 'threads'
threads.Threads.serialization('threads.sharedserialize')
local tds = require 'tds'
local threadTester = classic.class('threadTester')
function threadTester:_init(atomic)
self.game = threads.Threads(1,
function ()
inner = atomic
end)
classic.strict(self)
end
function threadTester:play()
self.game:addjob(function ()
while true do
if inner:get() < 0 then break end
end
end)
-- do some stuff outside
end
return threadTester
在macOS 10.12,火炬7中,拨打local threads = require 'threads'
threads.Threads.serialization('threads.sharedserialize')
local tds = require 'tds'
local signal = require 'posix.signal'
local ctrlpool = threads.Threads(1, function ()
local tds = require 'tds'
end)
local atomic = tds.AtomicCounter()
atomic:set(1)
nThreads = 4
local ctrlPool = threads.Threads(1)
ctrlPool:addjob(function ()
local signal = require 'posix.signal'
signal.signal(signal.SIGINT, function(signum)
print('\nSIGINT received')
print('Ex(c)iting')
atomic:set(-1)
end)
end)
ctrlPool:synchronize()
local gamePool = threads.Threads(nThreads, function ()
threadTester = require 'threadTester'
player = threadTester(atomic)
end)
for i = 1, nThreads do
gamePool:addjob(function ()
print(string.format("begin in thread %d", __threadid))
local status, err = xpcall(player.play, debug.traceback, player)
if not status then
print(string.format('%s', err))
os.exit(128)
end
end)
end
gamePool:synchronize()
gamePool:terminate()
,然后按th test.lua
,没有任何反应。
但如果我按以下方式更改ctrl+c
test.lua
一切都如预期的那样
local threads = require 'threads'
threads.Threads.serialization('threads.sharedserialize')
local tds = require 'tds'
local signal = require 'posix.signal'
-- local ctrlpool = threads.Threads(1, function ()
-- local tds = require 'tds'
-- end)
local atomic = tds.AtomicCounter()
atomic:set(1)
nThreads = 4
-- local ctrlPool = threads.Threads(1)
-- ctrlPool:addjob(function ()
local signal = require 'posix.signal'
signal.signal(signal.SIGINT, function(signum)
print('\nSIGINT received')
print('Ex(c)iting')
atomic:set(-1)
end)
-- end)
-- ctrlPool:synchronize()
local gamePool = threads.Threads(nThreads, function ()
threadTester = require 'threadTester'
player = threadTester(atomic)
end)
for i = 1, nThreads do
gamePool:addjob(function ()
print(string.format("begin in thread %d", __threadid))
local status, err = xpcall(player.play, debug.traceback, player)
if not status then
print(string.format('%s', err))
os.exit(128)
end
end)
end
gamePool:synchronize()
gamePool:terminate()
但如果我使用> th test.lua
begin in thread 1
begin in thread 2
begin in thread 3
begin in thread 4
^C
SIGINT received
Ex(c)iting
qlua
再次失败......
我很困惑......我不知道为什么它在>qlua test.lua
begin in thread 1
begin in thread 2
begin in thread 3
begin in thread 4
^C^C^C^C^C^C^Z
[3] + 15370 suspended qlua test.lua
中不起作用,我确实看到有人在ctrl池中注册信号处理程序......
如果我像这样更改qlua
threadTester.lua
它的功能更加奇怪(使用no-ctrl-pool local classic = require 'classic'
local threads = require 'threads'
threads.Threads.serialization('threads.sharedserialize')
local tds = require 'tds'
local threadTester = classic.class('threadTester')
function threadTester:_init(atomic)
self.game = threads.Threads(1,
function ()
inner = atomic
end)
self.atomic = atomic
classic.strict(self)
end
function threadTester:play()
self.game:addjob(function ()
os.execute("sleep ".. 10)
end)
-- do some stuff outside
while true do
if self.atomic:get() < 0 then break end
end
end
return threadTester
)
test.lua
似乎仅在th test.lua
begin in thread 1
begin in thread 2
begin in thread 3
begin in thread 4
^C^C^C^C^C
SIGINT received
Ex(c)iting
完成后才收到信号。