<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Error Multiprocessing in RF]]></title><description><![CDATA[<p dir="auto">Hi <a class="plugin-mentions-user plugin-mentions-a" href="https://forum.robofont.com/uid/1">@frederik</a> and <a class="plugin-mentions-user plugin-mentions-a" href="https://forum.robofont.com/uid/22">@gferreira</a>,</p>
<p dir="auto">I was trying to try out some parallelism in my code with RF. I'm running into a potential issue, I was wondering if it was a bug.</p>
<p dir="auto">The following two simple examples for <code>multiprocessing</code> are found on this page: <a href="https://realpython.com/python-concurrency/" rel="nofollow">https://realpython.com/python-concurrency/</a></p>
<pre><code class="language-python">import multiprocessing
import time


def cpu_bound(number):
    return sum(i * i for i in range(number))


def find_sums(numbers):
    with multiprocessing.Pool() as pool:
        pool.map(cpu_bound, numbers)


if __name__ == "__main__":
    numbers = [5_000_000 + x for x in range(20)]

    start_time = time.time()
    find_sums(numbers)
    duration = time.time() - start_time
    print(f"Duration {duration} seconds")
</code></pre>
<pre><code class="language-python">import requests
import multiprocessing
import time

session = None


def set_global_session():
    global session
    if not session:
        session = requests.Session()


def download_site(url):
    with session.get(url) as response:
        name = multiprocessing.current_process().name
        print(f"{name}:Read {len(response.content)} from {url}")


def download_all_sites(sites):
    with multiprocessing.Pool(initializer=set_global_session) as pool:
        pool.map(download_site, sites)


if __name__ == "__main__":
    sites = [
        "https://www.jython.org",
        "http://olympus.realpython.org/dice",
    ] * 80
    start_time = time.time()
    download_all_sites(sites)
    duration = time.time() - start_time
    print(f"Downloaded {len(sites)} in {duration} seconds")
</code></pre>
<p dir="auto">For both examples, the method that is called by the multiprocessing pool (<code>cpu_bound</code> in the first and <code>download_site</code> in the second) are met with the same <code>PicklingError</code>:</p>
<pre><code class="language-console">Traceback (most recent call last):
  File "&lt;untitled&gt;", line 18, in &lt;module&gt;
  File "&lt;untitled&gt;", line 11, in find_sums
  File "multiprocessing/pool.pyc", line 268, in map
  File "multiprocessing/pool.pyc", line 657, in get
  File "multiprocessing/pool.pyc", line 431, in _handle_tasks
  File "multiprocessing/connection.pyc", line 206, in send
  File "multiprocessing/reduction.pyc", line 51, in dumps
_pickle.PicklingError: Can't pickle &lt;function cpu_bound at 0x148c59320&gt;: attribute lookup cpu_bound on __main__ failed
</code></pre>
<p dir="auto">Both examples run in terminal and in my IDE A-OK.</p>
<p dir="auto">I was wondering if either of you would know what was going on here. Is there a way I should edit the examples above to get them to run, or is it a bug in RF?</p>
<p dir="auto">Thanks!</p>
]]></description><link>https://forum.robofont.com/topic/877/error-multiprocessing-in-rf</link><generator>RSS for Node</generator><lastBuildDate>Wed, 12 Aug 2026 03:23:07 GMT</lastBuildDate><atom:link href="https://forum.robofont.com/topic/877.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 12 Jun 2020 15:54:05 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Error Multiprocessing in RF on Fri, 12 Jun 2020 16:51:07 GMT]]></title><description><![CDATA[<p dir="auto">Hi <a class="plugin-mentions-user plugin-mentions-a" href="https://forum.robofont.com/uid/1">@frederik</a> and <a class="plugin-mentions-user plugin-mentions-a" href="https://forum.robofont.com/uid/22">@gferreira</a>,</p>
<p dir="auto">I was trying to try out some parallelism in my code with RF. I'm running into a potential issue, I was wondering if it was a bug.</p>
<p dir="auto">The following two simple examples for <code>multiprocessing</code> are found on this page: <a href="https://realpython.com/python-concurrency/" rel="nofollow">https://realpython.com/python-concurrency/</a></p>
<pre><code class="language-python">import multiprocessing
import time


def cpu_bound(number):
    return sum(i * i for i in range(number))


def find_sums(numbers):
    with multiprocessing.Pool() as pool:
        pool.map(cpu_bound, numbers)


if __name__ == "__main__":
    numbers = [5_000_000 + x for x in range(20)]

    start_time = time.time()
    find_sums(numbers)
    duration = time.time() - start_time
    print(f"Duration {duration} seconds")
</code></pre>
<pre><code class="language-python">import requests
import multiprocessing
import time

session = None


def set_global_session():
    global session
    if not session:
        session = requests.Session()


def download_site(url):
    with session.get(url) as response:
        name = multiprocessing.current_process().name
        print(f"{name}:Read {len(response.content)} from {url}")


def download_all_sites(sites):
    with multiprocessing.Pool(initializer=set_global_session) as pool:
        pool.map(download_site, sites)


if __name__ == "__main__":
    sites = [
        "https://www.jython.org",
        "http://olympus.realpython.org/dice",
    ] * 80
    start_time = time.time()
    download_all_sites(sites)
    duration = time.time() - start_time
    print(f"Downloaded {len(sites)} in {duration} seconds")
</code></pre>
<p dir="auto">For both examples, the method that is called by the multiprocessing pool (<code>cpu_bound</code> in the first and <code>download_site</code> in the second) are met with the same <code>PicklingError</code>:</p>
<pre><code class="language-console">Traceback (most recent call last):
  File "&lt;untitled&gt;", line 18, in &lt;module&gt;
  File "&lt;untitled&gt;", line 11, in find_sums
  File "multiprocessing/pool.pyc", line 268, in map
  File "multiprocessing/pool.pyc", line 657, in get
  File "multiprocessing/pool.pyc", line 431, in _handle_tasks
  File "multiprocessing/connection.pyc", line 206, in send
  File "multiprocessing/reduction.pyc", line 51, in dumps
_pickle.PicklingError: Can't pickle &lt;function cpu_bound at 0x148c59320&gt;: attribute lookup cpu_bound on __main__ failed
</code></pre>
<p dir="auto">Both examples run in terminal and in my IDE A-OK.</p>
<p dir="auto">I was wondering if either of you would know what was going on here. Is there a way I should edit the examples above to get them to run, or is it a bug in RF?</p>
<p dir="auto">Thanks!</p>
]]></description><link>https://forum.robofont.com/post/3388</link><guid isPermaLink="true">https://forum.robofont.com/post/3388</guid><dc:creator><![CDATA[colinmford]]></dc:creator><pubDate>Fri, 12 Jun 2020 16:51:07 GMT</pubDate></item><item><title><![CDATA[Reply to Error Multiprocessing in RF on Fri, 12 Jun 2020 16:11:22 GMT]]></title><description><![CDATA[<p dir="auto">have to dive into how RF executes py code, but I guess this solved when you run th py from a file that is saved...</p>
]]></description><link>https://forum.robofont.com/post/3389</link><guid isPermaLink="true">https://forum.robofont.com/post/3389</guid><dc:creator><![CDATA[frederik]]></dc:creator><pubDate>Fri, 12 Jun 2020 16:11:22 GMT</pubDate></item><item><title><![CDATA[Reply to Error Multiprocessing in RF on Fri, 12 Jun 2020 16:14:57 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="https://forum.robofont.com/uid/1">@frederik</a> Oh ok, I'll try that in a minute!</p>
]]></description><link>https://forum.robofont.com/post/3390</link><guid isPermaLink="true">https://forum.robofont.com/post/3390</guid><dc:creator><![CDATA[colinmford]]></dc:creator><pubDate>Fri, 12 Jun 2020 16:14:57 GMT</pubDate></item><item><title><![CDATA[Reply to Error Multiprocessing in RF on Fri, 12 Jun 2020 16:22:09 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="https://forum.robofont.com/uid/1">@frederik</a> No, it still gives the same error whether or not it is run from a file via the Scripts menu or through the Python Scripting Window :/</p>
]]></description><link>https://forum.robofont.com/post/3392</link><guid isPermaLink="true">https://forum.robofont.com/post/3392</guid><dc:creator><![CDATA[colinmford]]></dc:creator><pubDate>Fri, 12 Jun 2020 16:22:09 GMT</pubDate></item><item><title><![CDATA[Reply to Error Multiprocessing in RF on Fri, 12 Jun 2020 19:45:09 GMT]]></title><description><![CDATA[<p dir="auto">I see...</p>
<p dir="auto">asyncio examples does work... I assume, without doing to much research, the issue is related to the combo with pyObject and an already running runloop. RoboFont is one big runloop.</p>
]]></description><link>https://forum.robofont.com/post/3395</link><guid isPermaLink="true">https://forum.robofont.com/post/3395</guid><dc:creator><![CDATA[frederik]]></dc:creator><pubDate>Fri, 12 Jun 2020 19:45:09 GMT</pubDate></item><item><title><![CDATA[Reply to Error Multiprocessing in RF on Fri, 12 Jun 2020 20:37:55 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="https://forum.robofont.com/uid/1">@frederik</a> I see, so not much hope for this specific issue to be fixed?</p>
<p dir="auto">Essentially where I was hoping to go with this is I think it would be great to somehow run a process in the background without RoboFont locking up the interface. Even this simple <code>asyncio</code> example, while it does execute in parallel, causes RoboFont to "beachball" while the process completes.</p>
<pre><code class="language-python">import asyncio

async def count():
    print("One")
    await asyncio.sleep(10)
    print("Two")

async def main():
    await asyncio.gather(count(), count(), count())

if __name__ == "__main__":
    import time
    s = time.perf_counter()
    asyncio.run(main())
    elapsed = time.perf_counter() - s
    print(f"{__file__} executed in {elapsed:0.2f} seconds.")
</code></pre>
<p dir="auto">Is there any way to spawn a process or run a script, maybe on another core (I have 8 cores on this iMac, RoboFont only runs on 1), that runs in the background, and doesn't block RF's other processes?</p>
]]></description><link>https://forum.robofont.com/post/3396</link><guid isPermaLink="true">https://forum.robofont.com/post/3396</guid><dc:creator><![CDATA[colinmford]]></dc:creator><pubDate>Fri, 12 Jun 2020 20:37:55 GMT</pubDate></item><item><title><![CDATA[Reply to Error Multiprocessing in RF on Fri, 12 Jun 2020 20:49:27 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="https://forum.robofont.com/uid/1">@frederik</a> And for what it's worth, the <code>multiprocessing</code> scripts don't seem to work in the standalone Drawbot app either, same error. So whatever the issue is in RF, it seems like it's the same in DrawBot.</p>
<p dir="auto">I tried it in DrawBot because I seem to remember trying <code>multiprocessing</code> a year ago for a drawbot thing after seeing this post <a href="https://forum.drawbot.com/topic/150/drawbot-multiprocessing?_=1591994686928" rel="nofollow">https://forum.drawbot.com/topic/150/drawbot-multiprocessing?_=1591994686928</a></p>
<p dir="auto">and it worked back then! I wonder what changed?</p>
]]></description><link>https://forum.robofont.com/post/3397</link><guid isPermaLink="true">https://forum.robofont.com/post/3397</guid><dc:creator><![CDATA[colinmford]]></dc:creator><pubDate>Fri, 12 Jun 2020 20:49:27 GMT</pubDate></item><item><title><![CDATA[Reply to Error Multiprocessing in RF on Sat, 13 Jun 2020 13:09:56 GMT]]></title><description><![CDATA[<p dir="auto">asyncio support can be improved, will try out some bit and pieces.</p>
<p dir="auto">Threading, multiprocessing is harder with user interaction and with the bridge between python and cocoa...</p>
<p dir="auto">FontGoggles uses <a href="https://github.com/alberthier/corefoundationasyncio" rel="nofollow">https://github.com/alberthier/corefoundationasyncio</a></p>
]]></description><link>https://forum.robofont.com/post/3400</link><guid isPermaLink="true">https://forum.robofont.com/post/3400</guid><dc:creator><![CDATA[frederik]]></dc:creator><pubDate>Sat, 13 Jun 2020 13:09:56 GMT</pubDate></item><item><title><![CDATA[Reply to Error Multiprocessing in RF on Sat, 13 Jun 2020 13:26:37 GMT]]></title><description><![CDATA[<p dir="auto">why it worked in DrawBot, I dont know.. Could be the switch between py2 -&gt; py3.6 -&gt; py3.7... Do you have a DrawBot version number?</p>
]]></description><link>https://forum.robofont.com/post/3401</link><guid isPermaLink="true">https://forum.robofont.com/post/3401</guid><dc:creator><![CDATA[frederik]]></dc:creator><pubDate>Sat, 13 Jun 2020 13:26:37 GMT</pubDate></item><item><title><![CDATA[Reply to Error Multiprocessing in RF on Sat, 13 Jun 2020 13:34:48 GMT]]></title><description><![CDATA[<p dir="auto">found it!!</p>
<p dir="auto">in RF and DB the main module must be the py file starting the app!</p>
<pre><code>import sys

print(sys.modules["__main__"])
</code></pre>
<p dir="auto">while when you run your code in terminal the <code>__main__</code> module is the file itself...</p>
<p dir="auto">do an import of the your multiprocessing code instead.</p>
]]></description><link>https://forum.robofont.com/post/3402</link><guid isPermaLink="true">https://forum.robofont.com/post/3402</guid><dc:creator><![CDATA[frederik]]></dc:creator><pubDate>Sat, 13 Jun 2020 13:34:48 GMT</pubDate></item><item><title><![CDATA[Reply to Error Multiprocessing in RF on Sat, 13 Jun 2020 14:27:52 GMT]]></title><description><![CDATA[<p dir="auto">DrawBot PR to improve asyncio: <a href="https://github.com/typemytype/drawbot/pull/392" rel="nofollow">https://github.com/typemytype/drawbot/pull/392</a></p>
<p dir="auto">this allows to create a task in a run loop:</p>
<pre><code># will not work in RF3.4 or DB1.126 but will work in the next releases!! 
import asyncio

async def count():
    print("One")
    await asyncio.sleep(10)
    print("Two")

async def main():
    await asyncio.gather(count(), count(), count())

if __name__ == "__main__":
    asyncio.create_task(main())
</code></pre>
]]></description><link>https://forum.robofont.com/post/3403</link><guid isPermaLink="true">https://forum.robofont.com/post/3403</guid><dc:creator><![CDATA[frederik]]></dc:creator><pubDate>Sat, 13 Jun 2020 14:27:52 GMT</pubDate></item><item><title><![CDATA[Reply to Error Multiprocessing in RF on Sun, 14 Jun 2020 03:02:27 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="https://forum.robofont.com/uid/1">@frederik</a> Frederik, this is all brilliant!! Thanks so much for digging into it.</p>
<p dir="auto">Importing the multiprocessing code is indeed the trick; the demos work now perfectly.</p>
<p dir="auto">And I can't wait to try out the new async stuff in the next releases!</p>
<p dir="auto">Thanks!!</p>
]]></description><link>https://forum.robofont.com/post/3407</link><guid isPermaLink="true">https://forum.robofont.com/post/3407</guid><dc:creator><![CDATA[colinmford]]></dc:creator><pubDate>Sun, 14 Jun 2020 03:02:27 GMT</pubDate></item></channel></rss>