python - Send method to subprocess -
i'm implementing process pool using twisted. ultimate goal simple defertoprocess function, defertothread. i'm aware of ampoule module, i'm avoiding because it's apparently not maintained, because requires amp subclasses, , because i'm interested in writing anyway.
i want able call arbitrary static function in current project defertoprocess. twisted creates arbitrary process, got invoke current python interpreter, duplicated import path. find module level function determine module function resides in, import module , call function. occurred me might tricky pass function reference say, static class method or lambda.
to figure out options were, took @ python's multiprocessing module. tried worked perfectly, reference global object in parent process.
>>> class test(object): ... @staticmethod ... def test(): ... print 'found it!' ... >>> p = multiprocessing.process(target=test.test, args=()) >>> p.start() found it! >>> import sys >>> p = multiprocessing.process(target=lambda: sys.stdout.write('hello\r\n'), args=()) >>> p.start() hello >>> hm='testing' >>> p = multiprocessing.process(target=lambda: sys.stdout.write(hm), args=()) >>> p.start() testing
obviously python must fork child processes of data intact. since twisted spawns fresh processes, there reasonable way try recover full path call method, assuming 1 exists (e.g. method not dynamic)? includes static module level functions, lambda functions assigned module level variable, , static methods of classes. don't see way this
for instance, running hypothetical function
findcallpath(test.test)
might return
module.module.module.test.test
thanks in advance.
the function want twisted.python.reflect.fullyqualifiedname
, almost want:
>>> twisted.python.reflect import fullyqualifiedname >>> fullyqualifiedname(fullyqualifiedname) 'twisted.python.reflect.fullyqualifiedname'
however, fall short when interacting static methods:
>>> class something(object): ... @staticmethod ... def somethingelse(): ... pass ... >>> fullyqualifiedname(something.somethingelse) '__main__.somethingelse'
although arguably bug - we'd accept patch fix :).
the reverse operation twisted.python.reflect.namedany
(so called because there other functions namedclass
, namedmodule
, return type of thing):
>>> twisted.python.reflect import namedany >>> namedany(fullyqualifiedname(namedany)) namedany true
hope helps!
Comments
Post a Comment