# Efficiently interfacing Brian2 with external code - Bypassing NetworkOperation

**URL:** <https://brian.discourse.group/t/efficiently-interfacing-brian2-with-external-code-bypassing-networkoperation/915>\
**Category:** Feature requests\
**Tags:** performance\
**Created:** [20 February 2023 22:09 UTC](https://brian.discourse.group/t/efficiently-interfacing-brian2-with-external-code-bypassing-networkoperation/915 "2023-02-20T22:09:38Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![AlexPazzaglia](https://yyz2.discourse-cdn.com/free1/user_avatar/brian.discourse.group/alexpazzaglia/32/564_2.png) [@AlexPazzaglia](https://brian.discourse.group/u/AlexPazzaglia)\
**Post date:** [20 February 2023 22:09 UTC](https://brian.discourse.group/t/efficiently-interfacing-brian2-with-external-code-bypassing-networkoperation/915/1 "2023-02-20T22:09:38Z")

</div>

Hello, in my application I am interfacing a Brian2 network (built in Cython) with an external simulation that also runs in Cython and is called from Python code.  
The two simulations need to exchange data (network states and external input) every timestep.  
The only way I found to solve this problem is through the use of a NetworkOperation object that takes care of the communication happening via Python queues among two threads.  
While this solution works, it slows down the simulation drammatically.  
Could there be a way to provide a callback function directly at the Cython level and by-pass the NetworkOperation?

I think it would be very nice to have a feature such as a Python, Cython or even C++ - level q\_in/q\_out option

---

<div class="post-metadata">

**Author:** ![mstimberg](https://yyz2.discourse-cdn.com/free1/user_avatar/brian.discourse.group/mstimberg/32/11_2.png) [@mstimberg](https://brian.discourse.group/u/mstimberg)\
**Post date:** [21 February 2023 17:44 UTC](https://brian.discourse.group/t/efficiently-interfacing-brian2-with-external-code-bypassing-networkoperation/915/2 "2023-02-21T17:44:25Z")

</div>

Hi @AlexPazzaglia. Linking a Brian simulation to external code without having to use a `network_operation` is one of the things we want to make possible in Brian. In many cases, it should be quite straightforward using our “user-defined function” mechanism, which allows you to define a function directly in the target language: [Functions — Brian 2 2.5.1 documentation](https://brian2.readthedocs.io/en/stable/advanced/functions.html#other-code-generation-targets) Unfortunately, we currently only have one example showing how to use this mechanism (for C++): [Example: opencv\_movie — Brian 2 2.5.1 documentation](https://brian2.readthedocs.io/en/stable/examples/advanced.opencv_movie.html?highlight=%40implementation#example-opencv-movie)

I think the feature has been considerably less tested for Cython compared to C++, so please let us know if it doesn’t work for you or if anything is unclear for your particular use case.
