66425

how to use asyncio with boost.python?

Is it possible to use Python3 asyncio package with Boost.Python library?

I have CPython C++ extension that builds with Boost.Python. And functions that are written in C++ can work really long time. I want to use asyncio to call these functions but res = await cpp_function() code doesn't work.

    <li>What happens when cpp_function is called inside coroutine?</li> <li>How not get blocked by calling C++ function that works very long time?</li> </ul>

    <strong>NOTE</strong>: C++ doesn't do some I/O operations, just calculations.

    Answer1:

    What happens when cpp_function is called inside coroutine?

    If you call long-running Python/C function inside any of your coroutines, it freezes your event loop (freezes all coroutines everywhere).

    You should avoid this situation.

    How not get blocked by calling C++ function that works very long time

    You should use run_in_executor to run you function in separate thread or process. run_in_executor returns coroutine that you can await.

    You'll probably need ProcessPoolExecutor because of GIL (I'm not sure if ThreadPoolExecutor is option in your situation, but I advice you to check it).

    Here's example of awaiting long-running code:

    import asyncio from concurrent.futures import ProcessPoolExecutor import time def blocking_function(): # Function with long-running C/Python code. time.sleep(3) return True async def main(): # Await of executing in other process, # it doesn't block your event loop: loop = asyncio.get_event_loop() res = await loop.run_in_executor(executor, blocking_function) if __name__ == '__main__': executor = ProcessPoolExecutor(max_workers=1) # Prepare your executor somewhere. loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) try: loop.run_until_complete(main()) finally: loop.run_until_complete(loop.shutdown_asyncgens()) loop.close()

Recommend

  • Unity stop and start coroutines
  • Start coroutine on an inactive/de-activated GameObject
  • Update Android Studio Project to Kotlin 1.3
  • Optional deconstruction function parameter
  • Matlab code optimization and removing loops [closed]
  • Ordering of eigenvectors when calculating eigenvectors using LAPACK's ssteqr
  • How to create a thread in a class?
  • NSTimer and updating UI
  • TensorFlow C++, runtime issue
  • init_seg and warning C4073 from library code?
  • iOS Localization Doesn't Work with More Than 63 Files
  • Is there a Windows socket API call / option to “block” a range of ports à la SO_EXCLUSIVEADDRUSE
  • why calling cd shell command through system() or execvp() from a child process won't work?
  • Python cosine function precision [duplicate]
  • vectorized indexing/slicing in numpy/scipy?
  • Rest Services conventions
  • Android Activity.onWindowFocusChanged doesn't get called from within TabHost
  • QLineEdit password safety
  • How to recover from a Spring Social ExpiredAuthorizationException
  • Validaiting emails with Net.Mail MailAddress
  • ILMerge & Keep Assembly Name
  • Perl system calls when running as another user using sudo
  • Which linear programming package should I use for high numbers of constraints and “warm starts” [clo
  • Javascript + PHP Encryption with pidCrypt
  • Convert array of 8 bytes to signed long in C++
  • Large data - storage and query
  • Why winpcap requires both .lib and .dll to run?
  • WOWZA + RTMP + HTML5 Playback?
  • SVN: Merging two branches together
  • Hibernate gives error error as “Access to DialectResolutionInfo cannot be null when 'hibernate.
  • php design question - will a Helper help here?
  • How to set the response of a form post action to a iframe source?
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • Can Visual Studio XAML designer handle font family names with spaces as a resource?
  • Are Kotlin's Float, Int etc optimised to built-in types in the JVM? [duplicate]
  • unknown Exception android
  • Binding checkboxes to object values in AngularJs
  • Net Present Value in Excel for Grouped Recurring CF
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize
  • How to load view controller without button in storyboard?