I've ported my parallel hashcash program to Python -- since it's just a wrapper around the normal hashcash program. This version should be ready for general consumption. The script reads the stamp from its children via a pipe, so the multiple instances won't step on each other.
#!/usr/bin/python # hashfork.py # # by Hubert Chan, March 2005 # # This file is hereby placed in the public domain. import os import signal import sys if len(sys.argv) < 3: print "Run multiple instances of hashcash in parallel" print "usage:" print " ", sys.argv, "<processes> <hashcash command line arguments>" print print " <processes> is the number of hashcash processes to start" print " <hashcash command line arguments> is the command line argumens to pass to" print " hashcash. Note that the -m option will be automatically added, and" print " so does not need to be specified." print print "Note: you MUST specify a resource on the command line." sys.exit () NUM_PROCS = int (sys.argv) del (sys.argv[0:2]) sys.argv.insert(0, "hashcash") sys.argv.insert(1, "-m") children = [ ] pipes = [ ] for i in range (NUM_PROCS): pipe = os.pipe () # create a pipe to communicate with the child pid = os.fork () if pid: # parent children.append (pid) os.close (pipe) # close the write end of the pipe pipes.append (os.fdopen (pipe)) # print "process", pid, "started" else: # child os.close (pipe) # close the read end of the pipe os.dup2 (pipe, 1) # redirect stdout to the pipe os.close (pipe) os.execvp ("hashcash", sys.argv) status = os.wait () #print "process", status, "exited.\nkilling all children." for i in range (NUM_PROCS): # print children[i] if children[i] == status: print (pipes[i].readline ()), try: os.kill (children[i], signal.SIGKILL) except OSError: # kill raises an OSError when the process already exited pass
Note that this script will only work on UNIX systems because it uses the wait function, which is only available on UNIX. If anyone knows how to do the equivalent in Windows, let me know. I think execvp is only available under UNIX as well (but that can be easily gotten around by hard-coding the path to hashcash). -- Hubert Chan <hubert@xxxxxxxxx> - http://www.uhoreg.ca/ PGP/GnuPG key: 1024D/124B61FA Fingerprint: 96C5 012F 5F74 A5F7 1FF7 5291 AF29 C719 124B 61FA Key available at wwwkeys.pgp.net. Encrypted e-mail preferred.