unsync icon indicating copy to clipboard operation
unsync copied to clipboard

CPU Bound Decorator in Wrapped Functions

Open trin5tensa opened this issue 4 years ago • 1 comments

For context, I am a new user of unsync and I am exploring its use for the problem of running concurrent code triggered from a tkinter gui. I'm currently studying ways of getting data produced by concurrent operations back into tkinter's own event loop. As part of that I've come across a problem with cpu bound functions.

In the following code the cpu_bound_2 function fails with this exception:


Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/concurrent/futures/process.py", line 243, in _process_worker
    r = call_item.fn(*call_item.args, **call_item.kwargs)
  File "/Users/stephen/Documents/Coding/Scrapbook/env/lib/python3.9/site-packages/unsync/unsync.py", line 98, in _multiprocess_target
    return unsync.unsync_functions[func_name](*args, **kwargs)
KeyError: ('__main__', 'func')
"""

The above exception was the direct cause of the following exception:

KeyError: ('__main__', 'func')
import math
import sys
import time

from unsync import unsync


@unsync(cpu_bound=True)
def cpu_bound_1():
    for number in range(20_000_000):
        math.sqrt(number)
    print("Finishing cpu_bound_1")
    
    
def threadable():
    @unsync
    def func():
        time.sleep(3)
        print("Finishing threadable")
    return func


def cpu_bound_2():
    @unsync(cpu_bound=True)
    def func():
        for number in range(20_000_000):
            math.sqrt(number)
        print("Finishing cpu_bound_2")
    return func


def main():
    cpu_bound_1()
    threadable()()
    cpu_bound_2()()


if __name__ == '__main__':
    sys.exit(main())

Python 3.9 on macOS Big Sur 11.4.

trin5tensa avatar Jun 25 '21 10:06 trin5tensa

CPU bound functions are pickled by name, so they need to be defined in the global namespace. It's an annoying limitation, but not sure of a better way to do it.

I'll update the documentation to mention this.

alex-sherman avatar Jun 25 '21 22:06 alex-sherman