Prove multiprocessing works, although it is still racy in some places

This commit is contained in:
James R. Barlow
2017-05-23 16:32:13 -07:00
parent 75f2262659
commit 148b632b4f
3 changed files with 40 additions and 14 deletions
+9
View File
@@ -682,6 +682,7 @@ class PdfInfo:
"""
def __init__(self, infile):
self._infile = infile
self._pages = _pdf_get_all_pageinfo(infile)
@property
@@ -702,6 +703,14 @@ class PdfInfo:
def __repr__(self):
return "<PdfInfo('...'), page count={}>".format(len(self))
# def __getstate__(self):
# state = {'_infile': self._infile}
# return state
#
# def __setstate__(self, state):
# self._infile = state['_infile']
# self._pages = _pdf_get_all_pageinfo(self._infile)
def main():
import argparse
+5 -2
View File
@@ -52,6 +52,9 @@ class JobContext:
def __init__(self):
self.pdfinfo = None
def generate_pdfinfo(self, infile):
self.pdfinfo = PdfInfo(infile)
def get_pdfinfo(self):
"What we know about the input PDF"
return self.pdfinfo
@@ -72,8 +75,8 @@ class JobContext:
self.work_folder = work_folder
from multiprocessing.managers import BaseManager
class JobContextManager(BaseManager):
from multiprocessing.managers import SyncManager
class JobContextManager(SyncManager):
pass
+26 -12
View File
@@ -5,29 +5,43 @@ from multiprocessing import Process
from multiprocessing.managers import BaseProxy
def client(context):
assert isinstance(context, BaseProxy)
pdfinfo = context.get_pdfinfo()
page = pdfinfo[0]
page.rotation = 90
context.set_pdfinfo(pdfinfo)
def test_proxies(resources):
def test_jobcontext_proxy(resources):
# Prove that managers are set up correctly to share state among processes
manager = JobContextManager()
manager.register('JobContext', JobContext)
# Start the manager in a child process (or maybe thread)
manager.start()
# Tell the manager process to retrieve pdf info
context = manager.JobContext()
context.generate_pdfinfo(resources / 'graph.pdf')
pdfinfo = PdfInfo(resources / 'graph.pdf')
# Get a copy of that information for this process
pdfinfo = context.get_pdfinfo()
assert len(pdfinfo) == 1
assert pdfinfo[0].rotation == 0
# Update information and send back to manager
pdfinfo[0].rotation = 90
context.set_pdfinfo(pdfinfo)
del pdfinfo
# Retrieve again, ensure it stayed changed
pdfinfo2 = context.get_pdfinfo()
assert pdfinfo2[0].rotation == 90
# Start a new process which gets its own proxy object
def client(context):
assert isinstance(context, BaseProxy)
pdfinfo = context.get_pdfinfo()
page = pdfinfo[0]
assert page.rotation == 90
page.rotation += 90
context.set_pdfinfo(pdfinfo)
p = Process(target=client, args=(context,))
p.start()
p.join()
assert p.exitcode == 0, "Child process failed"
assert context.get_pdfinfo()[0].rotation == 90
assert context.get_pdfinfo()[0].rotation == 180