Skip to content

Commit

Permalink
use multiprocessing under linux and subprocess unter windows
Browse files Browse the repository at this point in the history
Conflicts:
	dragonpy/core/DragonPy.py
	dragonpy/core/configs.py
	dragonpy/cpu6809.py
  • Loading branch information
jedie committed Jul 17, 2014
1 parent 8302a89 commit c13a91a
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 43 deletions.
62 changes: 32 additions & 30 deletions dragonpy/core/DragonPy.py
Expand Up @@ -47,43 +47,45 @@ class Dragon(object):
def __init__(self, cfg):
self.cfg = cfg
self.periphery = cfg.periphery_class(cfg)
self.cfg.periphery = self.periphery
#self.cfg.periphery = self.periphery

listener = socket.socket()
listener.bind(("127.0.0.1", 0))
listener.listen(0)

bus_socket_host, bus_socket_port = listener.getsockname()

p = multiprocessing.Process(
target=cpu6809.start_CPU,
args=(self.cfg, True, bus_socket_host, bus_socket_port)
)
p.daemon = True
p.start()


# cmd_args = [
# sys.executable,
# "-m", "dragonpy.cpu6809",
## os.path.abspath(cpu6809.__file__),
# "--bus_socket_host=%s" % bus_socket_host,
# "--bus_socket_port=%i" % bus_socket_port,
# ]
# cmd_args += sys.argv[1:]
#
# root_path = os.path.abspath(
# os.path.join(os.path.dirname(dragonpy.__file__), "..")
# )
#
# print "Startup CPU with: %s in %s" % (" ".join(cmd_args), root_path)
# try:
# # XXX: use multiprocessing module here?
# self.core = subprocess.Popen(cmd_args,
# cwd=root_path
# )
# except:
# print_exc_plus()
process_args = (self.cfg, True, bus_socket_host, bus_socket_port)
#
# TODO: Using multiprocessing doesn't work under windows, yet.
#
# Problem are not pickleable objects from self.cfg
# http://www.python-forum.de/viewtopic.php?p=261671#p261671 (de)
#
# Test with:
# import pickle
# pickle.dumps(process_args)
#
if sys.platform != "win32":
p = multiprocessing.Process(
target=cpu6809.start_CPU, args=process_args
)
p.daemon = True
p.start()
else:
cmd_args = [sys.executable, "-m", "dragonpy.cpu6809",
"--bus_socket_host=%s" % bus_socket_host,
"--bus_socket_port=%i" % bus_socket_port,
]
cmd_args += sys.argv[1:]
root_path = os.path.abspath(
os.path.join(os.path.dirname(dragonpy.__file__), "..")
)
print "Startup CPU with: %s in %s" % (" ".join(cmd_args), root_path)
try:
self.core = subprocess.Popen(cmd_args, cwd=root_path)
except:
print_exc_plus()

rs, _, _ = select.select([listener], [], [], 2)
if not rs:
Expand Down
19 changes: 8 additions & 11 deletions dragonpy/core/configs.py
Expand Up @@ -89,7 +89,12 @@ def __init__(self, cmd_args):
self.display_cycle = cmd_args.display_cycle

# socket address for internal bus I/O:
self.bus = None # Will be set in cpu6809.start_CPU()
if cmd_args.bus_socket_host and cmd_args.bus_socket_port:
self.bus = True
self.bus_socket_host = cmd_args.bus_socket_host
self.bus_socket_port = cmd_args.bus_socket_port
else:
self.bus = None # Will be set in cpu6809.start_CPU()

if cmd_args.ram:
self.ram = cmd_args.ram
Expand Down Expand Up @@ -148,18 +153,10 @@ def print_debug_info(self):
print "%20s = %s" % (name, value)










def test_run():
import sys, subprocess
import os, sys, subprocess
cmd_args = [sys.executable,
"DragonPy_CLI.py",
os.path.join("..", "..", "DragonPy_CLI.py"),

# "--verbosity=5",
"--verbosity=10", # DEBUG
Expand Down
21 changes: 19 additions & 2 deletions dragonpy/cpu6809.py
Expand Up @@ -2540,5 +2540,22 @@ def test_run():


if __name__ == "__main__":
test_run()

#
# TODO: Only needed for windows-multiprocessing-work-a-round:
# start with script with subprocess.Popen()
#
# See: dragonpy.core.DragonPy
#
from dragonpy.DragonPy_CLI import get_cli
cli = get_cli()

if cli.cfg.bus is None:
print "DragonPy cpu core"
print "Run DragonPy_CLI.py instead"
test_run()
sys.exit(0)

bus_socket_host = cli.cfg.bus_socket_host
bus_socket_port = cli.cfg.bus_socket_port

start_CPU(cli.cfg, True, bus_socket_host, bus_socket_port)

0 comments on commit c13a91a

Please sign in to comment.