# Multiprocessing Python/Q

**URL:** <https://forum.kx.com/t/multiprocessing-python-q/11951>\
**Category:** Community Support\
**Tags:** kdb-and-q\
**Created:** [September 27, 2019, 5:03pm UTC](https://forum.kx.com/t/multiprocessing-python-q/11951 "2019-09-27T17:03:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Anas](https://avatars.discourse-cdn.com/v4/letter/a/7ab992/32.png) [@Anas](https://forum.kx.com/u/Anas)\
**Post date:** [September 27, 2019, 5:03pm UTC](https://forum.kx.com/t/multiprocessing-python-q/11951/1 "2019-09-27T17:03:00Z")

</div>

Hello Everyone,

I am trying to use the multiprocessing capabilities of python to launch Q function on a server. But it seems that the processes cannot run in parallel. Indeed, using 1 or more processes does not change the running time.

the code looks like this:

if \_\_name\_\_== ‘\_\_main\_\_’:

&nbsp; &nbsp;&nbsp;

&nbsp; &nbsp; pool = mp.Pool(processes=3)

&nbsp; &nbsp; start\_time = time.time()&nbsp; &nbsp;&nbsp;

&nbsp; &nbsp; results = [pool.apply\_async(Launch\_Data\_Extraction, args=(x,)) for x in list\_]

&nbsp; &nbsp; output = [p.get() for p in results]

&nbsp; &nbsp; print(time.time()-start\_time)

&nbsp;&nbsp;

And Launch\_Data\_Extraction looks like&nbsp;

def Launch\_Data\_Extraction(x):

&nbsp; &nbsp;hdb = qconnection.QConnection(host=, port=,username=, pandas=True)

&nbsp; &nbsp;DO STUFF

&nbsp; &nbsp;return

x would be individual orders for example, they only share the same initial data in the server. I am essentially trying to multi process a for-loop.

Am I doing something wrong ? Someone can help on this please ?

Thanks in advance,

Anas

---

<div class="post-metadata">

**Author:** ![sandy\_bradley](https://avatars.discourse-cdn.com/v4/letter/s/ec9cab/32.png) [@sandy\_bradley](https://forum.kx.com/u/sandy_bradley)\
**Post date:** [September 28, 2019, 12:05pm UTC](https://forum.kx.com/t/multiprocessing-python-q/11951/2 "2019-09-28T12:05:00Z")

</div>

Hi Anas,

\<o:p\> \</o:p\>

Im not entirely clear what you are trying to do but I would use kdb directly, with slaves, then call peach for parallel processing, e.g.

\<o:p\> \</o:p\>

Start q with 3 slaves, on port 5001

q -s 3 -p 5001

\<o:p\> \</o:p\>

Define and call your data extraction function from q

Launch\_Data\_Extraction: { DO-STUFF x } ;

Launch\_Data\_Extraction peach list\_ ;

\<o:p\> \</o:p\>

\<o:p\> \</o:p\>

\<o:p\> \</o:p\>

**From:** a im  
**Sent:** Friday, 27 September 2019 18:04  
**To:** Kdb+ Personal Developers  
**Subject:** [personal kdb+] Multiprocessing Python/Q

\<o:p\> \</o:p\>

Hello Everyone,

\<o:p\> \</o:p\>

\<o:p\> \</o:p\>

I am trying to use the multiprocessing capabilities of python to launch Q function on a server. But it seems that the processes cannot run in parallel. Indeed, using 1 or more processes does not change the running time.

\<o:p\> \</o:p\>

the code looks like this:

\<o:p\> \</o:p\>

\<o:p\> \</o:p\>

\<o:p\> \</o:p\>

if \_\_name\_\_== ‘\_\_main\_\_’:

&nbsp; &nbsp;&nbsp;

&nbsp; &nbsp; pool = mp.Pool(processes=3)

&nbsp; &nbsp; start\_time = time.time()&nbsp; &nbsp;&nbsp;

&nbsp; &nbsp; results = [pool.apply\_async(Launch\_Data\_Extraction, args=(x,)) for x in list\_]

&nbsp; &nbsp; output = [p.get() for p in results]

&nbsp; &nbsp; print(time.time()-start\_time)

&nbsp;&nbsp;

\<o:p\> \</o:p\>

And Launch\_Data\_Extraction looks like&nbsp;

\<o:p\> \</o:p\>

def Launch\_Data\_Extraction(x):

&nbsp; &nbsp;hdb = qconnection.QConnection(host=, port=,username=, pandas=True)

&nbsp; &nbsp;DO STUFF

&nbsp; &nbsp;return

\<o:p\> \</o:p\>

x would be individual orders for example, they only share the same initial data in the server. I am essentially trying to multi process a for-loop.

\<o:p\> \</o:p\>

Am I doing something wrong ? Someone can help on this please ?

\<o:p\> \</o:p\>

Thanks in advance,

Anas

\<o:p\> \</o:p\>

\<o:p\> \</o:p\>

–  
Submitted via Google Groups
