-
-
Save 2called-chaos/16aaab2aa6533fb2360b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$deferred_importer = Mutex.new | |
$importing = [] | |
$importing.extend(MonitorMixin) | |
def aquire_deferred_lock | |
t = Thread.new{ | |
loop do | |
Thread.current[:lock] = $deferred_importer if $deferred_importer.try_lock | |
break if Thread.current[:lock] | |
sleep 1 | |
end | |
Thread.current[:lock].lock rescue `say moe` # it raises here so IT LOCKED | |
} | |
end | |
def wait_for_workers | |
t = Thread.new{ | |
loop do | |
l = $importing.synchronize { $importing.length } | |
return if l == 0 | |
sleep 1 | |
end | |
} | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ================================== | |
# = Wait for other workers to exit = | |
# ================================== | |
@status = ["waiting for other workers...", :yellow] | |
wchannel = var.wait_for_workers | |
second_progress(wchannel, "waiting for other workers (:seconds)...").join | |
may_interrupt | |
# ================= | |
# = Aquiring lock = | |
# ================= | |
@status = ["waiting for deferred import lock...", :yellow] | |
lchannel = var.aquire_deferred_lock | |
second_progress(lchannel, "waiting for deferred import lock (:seconds)...").join | |
may_interrupt | |
# Do some importing here (only one thread should be able to do this at once) | |
# My problem is that this doesn't seem to work, they get executed simultaneously | |
lchannel[:lock].unlock # they can't unlock though, they just keep hanging here as opposed to when aquiring it... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment