Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

multiprocessing and ctypes with pointers

I have multiProcessing.Process objects whose target functions take input and output queue.

To the output queue they put some data, that is a wrapped ctypes structure with internal pointers. Of course, the pickle module, that should serialize the data, breaks:

ValueError: ctypes objects containing pointers cannot be pickled

Can I somehow get my ctypes structures with pointers out of my child processes without dumping them to files?

The code is below

# -*- 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()
like image 408
Felix Avatar asked Sep 24 '13 08:09

Felix


People also ask

What is Ctypes pointer?

ctypes is a foreign function library for Python. It provides C compatible data types, and allows calling functions in DLLs or shared libraries. It can be used to wrap these libraries in pure Python.

What is multiprocessing pool?

The Python Multiprocessing Pool class allows you to create and manage process pools in Python. Although the Multiprocessing Pool has been available in Python for a long time, it is not widely used, perhaps because of misunderstandings of the capabilities and limitations of Processes and Threads in Python.

What is multiprocessing in Python?

Multiprocessing in Python is a built-in package that allows the system to run multiple processes simultaneously. It will enable the breaking of applications into smaller threads that can run independently.

How do you stop a multiprocessing process?

A process can be killed by calling the Process. terminate() function. The call will only terminate the target process, not child processes. The method is called on the multiprocessing.


1 Answers

When you use multiprocessing every process has its own address space. The pointer would not be valid in another process.

translate the object to a python object or to a ctypes type without pointers and it should work.

Keep in mind that changes to the object that occur in the other process will not be reflected in the parent unless you send the object back on the queue.

like image 122
Shani Avatar answered Oct 13 '22 11:10

Shani