Skip to content

Commit

Permalink
Pulling in fix for signal handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
liffiton committed Mar 27, 2015
2 parents 58996a0 + 7edabf5 commit a6393dc
Showing 1 changed file with 25 additions and 17 deletions.
42 changes: 25 additions & 17 deletions marco.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import atexit
import signal
import sys
import threading

import utils
import mapsolvers
Expand Down Expand Up @@ -218,23 +219,30 @@ def main():
sys.stderr.write("Result limit reached.\n")
sys.exit(0)

# enumerate results
remaining = args.limit

for result in mp.enumerate():
output = result[0]
if args.alltimes:
output = "%s %0.3f" % (output, stats.current_time())
if args.verbose:
output = "%s %s" % (output, " ".join([str(x + 1) for x in result[1]]))

print(output)

if remaining:
remaining -= 1
if remaining == 0:
sys.stderr.write("Result limit reached.\n")
sys.exit(0)
# enumerate results in a separate thread so signal handling works while in C code
# ref: https://thisismiller.github.io/blog/CPython-Signal-Handling/
def enumerate():
remaining = args.limit

for result in mp.enumerate():
output = result[0]
if args.alltimes:
output = "%s %0.3f" % (output, stats.current_time())
if args.verbose:
output = "%s %s" % (output, " ".join([str(x + 1) for x in result[1]]))

print(output)

if remaining:
remaining -= 1
if remaining == 0:
sys.stderr.write("Result limit reached.\n")
sys.exit(0)

enumthread = threading.Thread(target=enumerate)
enumthread.daemon = True # so thread is killed when main thread exits (e.g. in signal handler)
enumthread.start()
enumthread.join(float("inf")) # timeout required for signal handler to work; set to infinity


if __name__ == '__main__':
Expand Down

0 comments on commit a6393dc

Please sign in to comment.