<div dir="auto"><span style="font-family:sans-serif;font-size:12.8px">Hi.</span><div dir="auto" style="font-family:sans-serif;font-size:12.8px"><br></div><div dir="auto" style="font-family:sans-serif;font-size:12.8px">I am posting my reply again because I missed the qgis-developer mail list first...</div><div dir="auto" style="font-family:sans-serif;font-size:12.8px"><br></div><div dir="auto" style="font-family:sans-serif;font-size:12.8px">It is indeed possible to achieve multiprocessing in QGIS. </div><div dir="auto" style="font-family:sans-serif;font-size:12.8px"><br></div><div dir="auto" style="font-family:sans-serif;font-size:12.8px">But you should rely on Qt multi thread mechanisms rather than on the python multiprocessing module.</div><div dir="auto" style="font-family:sans-serif;font-size:12.8px"><br></div><div dir="auto" style="font-family:sans-serif;font-size:12.8px">Qgis is written in C++, based on the Qt library. Using the Qt possibilities, even if it is thru python, is less likely to trigger problems...</div><div dir="auto" style="font-family:sans-serif;font-size:12.8px"><br></div><div dir="auto" style="font-family:sans-serif;font-size:12.8px">QThread is your friend here.</div><div dir="auto" style="font-family:sans-serif;font-size:12.8px">You can also use classes that are designed in QGis to ease things up, like QgsTask (<a href="https://nocache.qgis.org/api/3.4/classQgsTask.html">https://nocache.qgis.org/api/3.4/classQgsTask.html</a>).</div><div dir="auto" style="font-family:sans-serif;font-size:12.8px"><br></div><div dir="auto" style="font-family:sans-serif;font-size:12.8px">Regards</div><div style="color:rgb(136,136,136);font-family:sans-serif;font-size:12.8px" dir="auto"><div dir="auto">Florian.</div></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">Le sam. 27 mars 2021 à 13:48, Joao Folgado <<a href="mailto:jfolgado94@gmail.com">jfolgado94@gmail.com</a>> a écrit :<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="auto">Hi guys,</div><div dir="auto"><br></div><div dir="auto">First of all thank you but, David Marteua and Sebastian Ernst, for the answer.</div><div dir="auto">So is impossible to use multiprocessing in Qgis plugin?</div><div dir="auto">This is a enormous limitations because Qgis have a lot of data  that  needs to be evaluated in same cases. Nowadays the cpu have 5,7, 9 cores and we can not use them in a plugin for example is bad. If some one have any idea who we can use multiprocessing in a plugin i will be thankful.</div><div dir="auto"><br></div><div dir="auto">Best regards,</div><div dir="auto">João Folgado</div><div dir="auto"> </div><div><div class="gmail_quote"><div dir="ltr" class="gmail_attr">David Marteau <<a href="mailto:dmarteau@3liz.com" target="_blank" rel="noreferrer">dmarteau@3liz.com</a>> escreveu em sex., 26/03/2021 às 08:57 :<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;padding-left:1ex;border-left-color:rgb(204,204,204)">> I am not entirely sure what QGIS' (intended) behavior is in a case like<br>
> this. From the top of my head, having read the relevant portion of its<br>
> source code, I'd expect at least a second application window likely<br>
> followed by non-deterministic behavior (again, depending on the<br>
> use-case, likely resulting in a crash). Can someone elaborate?<br>
<br>
I can confirm that the result is non-deterministic even with headless <br>
PyQgis programs.<br>
<br>
For programs relying heavily on multiprocessing the best strategy is to <br>
use a `fork-server` process<br>
started at the very beginning (before initializing Qgis) managing the <br>
pool of child processes, so that each forked<br>
process starts in the same state.<br>
<br>
David,<br>
<br>
Le 23/03/2021 à 19:22, Sebastian M. Ernst a écrit :<br>
> Hi João, all,<br>
><br>
> really interesting question.<br>
><br>
>> however when i run the plugin a new instance of application is open<br>
>> and give me an error.<br>
> I assume that you are using `multiprocessing` [1] from Python's standard<br>
> library. Its default behavior is to use process-based parallelism (as<br>
> the title in the documentation says). I am assuming that you have not<br>
> altered its default behavior (e.g. by configuring it to use threads<br>
> instead). This is why you get at least another "worker" process if you<br>
> e.g. create a process pool. I have successfully used this method with<br>
> QGIS before, but always on Linux and OS X. I am just guessing that you<br>
> are doing this on Windows, right?<br>
><br>
> Depending on the operating system, `multiprocessing` uses different<br>
> methods to create worker processes. On Linux and OS X, it simply forks<br>
> [2] the main process. This way, you do not see a second application<br>
> window popping up. You "simply" get a second, third, fourth, ... process<br>
> "in the background". On Windows, the `fork` syscall does not exist,<br>
> neither does something similar [3]. `multiprocessing` therefore starts a<br>
> complete new process from scratch. If it was pure Python, it would<br>
> simply start a new Python interpreter process. Because it is QGIS, it<br>
> opens another instance of QGIS.<br>
><br>
> I am not entirely sure what QGIS' (intended) behavior is in a case like<br>
> this. From the top of my head, having read the relevant portion of its<br>
> source code, I'd expect at least a second application window likely<br>
> followed by non-deterministic behavior (again, depending on the<br>
> use-case, likely resulting in a crash). Can someone elaborate?<br>
><br>
> Best regards,<br>
> Sebastian<br>
><br>
><br>
> 1: <a href="https://docs.python.org/3/library/multiprocessing.html" rel="noreferrer noreferrer" target="_blank">https://docs.python.org/3/library/multiprocessing.html</a><br>
> 2: <a href="https://en.wikipedia.org/wiki/Fork_(system_call)" rel="noreferrer noreferrer" target="_blank">https://en.wikipedia.org/wiki/Fork_(system_call)</a><br>
> 3: <a href="https://stackoverflow.com/q/985281/1672565" rel="noreferrer noreferrer" target="_blank">https://stackoverflow.com/q/985281/1672565</a><br>
><br>
><br>
> Am 23.03.21 um 13:08 schrieb Joao Folgado:<br>
>> Hi everyone,<br>
>><br>
>> I had created  a plugin for Qgis and I trying to optimize it. So i use<br>
>> in my code the library python that have Queue whit multiprocessing<br>
>> however when i run the plugin a new instance of application is open and<br>
>> give me an error.<br>
>> I had early use this type of multiprocessing before in simple projects<br>
>> in university and it works ok.<br>
>> Can someone help ? I researched in many forums, i see people whit the<br>
>> same question but not with the answer.<br>
>> Thank you very much.<br>
>><br>
>> Best regards,<br>
>> João Folgado<br>
>> Portugal<br>
>> -- <br>
>> JOÃO FOLGADO<br>
>><br>
>> _______________________________________________<br>
>> QGIS-Developer mailing list<br>
>> <a href="mailto:QGIS-Developer@lists.osgeo.org" target="_blank" rel="noreferrer">QGIS-Developer@lists.osgeo.org</a><br>
>> List info: <a href="https://lists.osgeo.org/mailman/listinfo/qgis-developer" rel="noreferrer noreferrer" target="_blank">https://lists.osgeo.org/mailman/listinfo/qgis-developer</a><br>
>> Unsubscribe: <a href="https://lists.osgeo.org/mailman/listinfo/qgis-developer" rel="noreferrer noreferrer" target="_blank">https://lists.osgeo.org/mailman/listinfo/qgis-developer</a><br>
>><br>
> _______________________________________________<br>
> QGIS-Developer mailing list<br>
> <a href="mailto:QGIS-Developer@lists.osgeo.org" target="_blank" rel="noreferrer">QGIS-Developer@lists.osgeo.org</a><br>
> List info: <a href="https://lists.osgeo.org/mailman/listinfo/qgis-developer" rel="noreferrer noreferrer" target="_blank">https://lists.osgeo.org/mailman/listinfo/qgis-developer</a><br>
> Unsubscribe: <a href="https://lists.osgeo.org/mailman/listinfo/qgis-developer" rel="noreferrer noreferrer" target="_blank">https://lists.osgeo.org/mailman/listinfo/qgis-developer</a><br>
_______________________________________________<br>
QGIS-Developer mailing list<br>
<a href="mailto:QGIS-Developer@lists.osgeo.org" target="_blank" rel="noreferrer">QGIS-Developer@lists.osgeo.org</a><br>
List info: <a href="https://lists.osgeo.org/mailman/listinfo/qgis-developer" rel="noreferrer noreferrer" target="_blank">https://lists.osgeo.org/mailman/listinfo/qgis-developer</a><br>
Unsubscribe: <a href="https://lists.osgeo.org/mailman/listinfo/qgis-developer" rel="noreferrer noreferrer" target="_blank">https://lists.osgeo.org/mailman/listinfo/qgis-developer</a><br>
</blockquote></div></div>-- <br><div dir="ltr" data-smartmail="gmail_signature"><div dir="ltr"><font size="4">JOÃO FOLGADO</font><br><div><div><span style="DISPLAY:block"><span style="DISPLAY:inline-block;WORD-WRAP:normal;FONT-SIZE:10pt"></span></span></div></div></div></div>
_______________________________________________<br>
QGIS-Developer mailing list<br>
<a href="mailto:QGIS-Developer@lists.osgeo.org" target="_blank" rel="noreferrer">QGIS-Developer@lists.osgeo.org</a><br>
List info: <a href="https://lists.osgeo.org/mailman/listinfo/qgis-developer" rel="noreferrer noreferrer" target="_blank">https://lists.osgeo.org/mailman/listinfo/qgis-developer</a><br>
Unsubscribe: <a href="https://lists.osgeo.org/mailman/listinfo/qgis-developer" rel="noreferrer noreferrer" target="_blank">https://lists.osgeo.org/mailman/listinfo/qgis-developer</a><br>
</blockquote></div>