c# - Python .NET, multithreading and the windows event loop -


i'm building python api around black box .net dll using python .net. dll doing networking operations. dll require me run windows message pumping loop, otherwise network operations stuck after while. run windows message loop using system.windows.forms.application.run() in main thread. works fine receiving data. program starts behave weirdly though when start calls other python thread dll. think related threading since problems accuring irregular - networking events disappear or come in late. far know python , c# thread safe, maybe because of multiple layers of wrapping goes wrong.

so have couple of questions:

  • is bad idea calls dll's multiple python theads? or depend on dll internals? thought dll's perspective python threads seen 1 single agents due gil.

  • does every python thread using dll need message pump?

  • it idea keep dll interactions in 1 thread. have difficulties accomplishing this, since give control message pump loop in main thread. naive approach put new outgoing network messages generated in worker thread on python queue, create custom message pump loop in main thread windows event handling monitors queue , if there message call dll. feels quite clunky , lot of work such simple task. right approach?

  • is there other way put function in main windows event loop monitors described queue , takes action? or should dive .net specifics , start using .net events or dispatchers?

i have found answers of own questions.

  • i think can't assume dll thread safe, best isolate interaction dll 1 thread.

  • it looks windows message system per thread , not per process, yes, every thread using windows message system required have windows message processing loop.

  • one can insert execution in windows event loop using form.invoke. running non-ui main loop, can dispatcher using dispatcher.currentdispatcher can used 'invoking' function. invoked function executed on thread dispatcher created. called function needs delegated though, c# specific thing pass references functions.

in end did non-ui main loop:

import clr import threading  # need access threading assembly clr.addreference("c:\\program files\\reference assemblies\\microsoft\\framework\\v3.0\\windowsbase.dll")  import system system.windows.threading import dispatcher system.windows.forms import application  # dispatcher current thread dispatcher = dispatcher.currentdispatcher  def display():     print(threading.current_thread())  # make deligate deligate = system.action(display)  def other_thread(dispatcher):     # can use dispatcher main thread     # schedule run other thread     dispatcher.invoke(deligate)  thread = threading.thread(target=other_thread, args=(dispatcher,)) thread.start() application.run() 

some related links:


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -