2
我有multiProcessing.Process
對象,其目標函數採用輸入和輸出隊列。多處理和帶指針的ctypes
給輸出隊列放置一些數據,這是一個帶有內部指針的包裝ctypes結構。當然,pickle
模塊,即應序列數據,打破:
ValueError: ctypes objects containing pointers cannot be pickled
我可以以某種方式得到我的ctypes使用指針結構從我的子進程沒有他們傾倒的文件?
的代碼如下
# -*- coding: utf-8 -*-
import multiprocessing as mp
from liblinear import *
from liblinearutil import *
def processTarget(inQueue, outQueue):
while(not inQueue.empty()):
inVal = inQueue.get()
#training model
y, x = [1,-1], [{1:inVal, 3:3*inVal}, {1:-1,3:-1}]
prob = problem(y, x)
param = parameter('-c 4 -B 1')
m = train(prob, param)
outQueue.put((inVal * 2, m))
print "done", inVal
inQueue.task_done()
def Main():
processes = []
inQueue = mp.JoinableQueue()
for i in xrange(10):
inQueue.put(i)
outQueue = mp.JoinableQueue()
for i in xrange(5):
process = mp.Process(target=processTarget, args=(inQueue, outQueue))
print "starting", i
process.start()
print "started", i
inQueue.join()
print "JOINED"
while(not outQueue.empty()):
print outQueue.get()
if __name__ == '__main__':
Main()