Multithreading use of python scripts in c++ -
i'm using code python multithreading scripts (got web):
class pymainguard { public: pymainguard() { py_initialize(); pyeval_initthreads(); mgilstate = pygilstate_ensure(); mthreadstate = pyeval_savethread(); } ~pymainguard() { pyeval_restorethread( mthreadstate ); pygilstate_release( mgilstate ); py_finalize(); } private: pygilstate_state mgilstate; pythreadstate* mthreadstate; }; class pythrguard { public: pythrguard() { mmaingilstate = pygilstate_ensure(); moldthreadstate = pythreadstate_get(); mnewthreadstate = py_newinterpreter(); pythreadstate_swap( mnewthreadstate ); msubthreadstate = pyeval_savethread(); msubgilstate = pygilstate_ensure(); } ~pythrguard() { pygilstate_release( msubgilstate ); pyeval_restorethread( msubthreadstate ); py_endinterpreter( mnewthreadstate ); pythreadstate_swap( moldthreadstate ); pygilstate_release( mmaingilstate ); } private: pygilstate_state mmaingilstate; pythreadstate* moldthreadstate; pythreadstate* mnewthreadstate; pythreadstate* msubthreadstate; pygilstate_state msubgilstate; };
in main thread need create pymainguard, , in every single thread have create pythrguard - creates there independent interpretator, allows me use threading without problem. still share same resources.
for example, if use scrypts:
import win32evtlog import time server = "localhost" logtype = "testeventsource" archfile = "c:\test.txt" eventlog_success = 0 hwritelog = win32evtlog.registereventsource(none, logtype.decode('unicode-escape')) strings = [] x in range(0, 5): del strings[:] y in range(0, 3): = "python run [%d] part %d" % (x, y) strings.append(a.decode('unicode-escape')) time.sleep(2) win32evtlog.reportevent(hwritelog, eventlog_success, 0, 515, none, strings, none) win32evtlog.deregistereventsource(hwritelog)
sometimes can crash, there can created events in windows log more or less 3 lines of text (i.e. different threads write 1 event, having same eventhandle, guess).
everything looks threads still works in same interpeter, not in different ones. or there chance win32evtlog thread unsafe, loads ones (not every interpreter, want) , creates such problems.
question is: how achieve different interpreters each thread?
Comments
Post a Comment