Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using multiprocessing package #119

Closed
zssng opened this issue Sep 26, 2024 · 8 comments
Closed

Using multiprocessing package #119

zssng opened this issue Sep 26, 2024 · 8 comments

Comments

@zssng
Copy link

zssng commented Sep 26, 2024

I want to define a command to execute a multiprocessing function, but the result is not executable, and the command line window keeps popping up. In Python on Windows, you need to addif __name__ == "__main__": import multiprocessing multiprocessing.freeze_support()to prevent the process creation code from being executed indefinitely. However, I don't know how to add it in the plugin code, or if there are other solutions?

@cunnane
Copy link
Owner

cunnane commented Sep 30, 2024

Try running this before you invoke any multiprocessing code:

   multiprocessing.set_executable(sys.executable.replace("python.exe", "pythonw.exe"))

No need to call freeze_support.

@zssng
Copy link
Author

zssng commented Oct 5, 2024

Thank you, this method works!

@zssng zssng closed this as completed Oct 5, 2024
@zssng
Copy link
Author

zssng commented Oct 6, 2024

Thank you, this method works!

When using the Python multiprocessing method in the plugin, it freezes with no response. Using this method only hides the popped-up window, but the code still does not execute correctly

@zssng zssng reopened this Oct 6, 2024
@cunnane
Copy link
Owner

cunnane commented Oct 7, 2024

Please try this test code. The function should return the input string after 3 seconds.

import xloil as xlo
import concurrent.futures
import time
import multiprocessing
import sys

multiprocessing.set_executable(sys.executable.replace("python.exe", "pythonw.exe"))

_EXECUTOR = concurrent.futures.ProcessPoolExecutor()

def do_nothing(sleep, result):
    time.sleep(sleep)
    return result
    
    
@xlo.func(rtd=True)
async def echo_multiprocess(message: str):
    future = xlo.get_async_loop().run_in_executor(_EXECUTOR, do_nothing, 3, message)
    return await future

@zssng
Copy link
Author

zssng commented Oct 9, 2024

image

@cunnane
Copy link
Owner

cunnane commented Oct 9, 2024

I can't replicate this, what version and distribution of python and packages are you using? Do you have any security software running? Are you loading anything else other than the above test code?

@cunnane cunnane changed the title Call multiprocssing method on Windows Using multiprocessing package Oct 9, 2024
@zssng
Copy link
Author

zssng commented Oct 10, 2024

Python 3.12.6 (tags/v3.12.6:a4a2d2b, Sep 6 2024, 20:11:23) [MSC v.1940 64 bit (AMD64)] on win32,No security software,xloil 0.19.1

@cunnane
Copy link
Owner

cunnane commented Oct 10, 2024

I'm also using that python version. Are you using a python distribution or vanilla python? Are you loading any other modules other than the above code? Are any other addins loaded in Excel? Which version of Excel? Windows defender runs by default in current windows versions, so when you say you have no security software, are you disabling this? Does any xloil log information appear (when logging is set to debug) other than the message above? If you do not set the executable to "pythonw.exe", does the test code run (with a popup)?

@zssng zssng closed this as completed Jan 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants