开发者

How to import the Python async module from a worker thread?

开发者 https://www.devze.com 2023-01-14 22:03 出处:网络
I\'m using the GitPython package to access a Git repository from Python. This pulls in the async package. In async/__init__.py, the following happens:

I'm using the GitPython package to access a Git repository from Python. This pulls in the async package. In async/__init__.py, the following happens:

def _init_signals():
    """Assure we shutdown our threads correctly when being interrupted"""
    import signal
    # ...
    signal.signal(signal.SIGINT, thread_interrupt_handler)

_init_signals()

This works fine if everything i开发者_Python百科s in the main thread. However, when the first import of git (and thus async) happens on another thread, things go boom:

ValueError: signal only works in main thread

Since all this runs inside the Django framework, I have no control over threading.

One workaround I've found is to put import async into settings.py, which is (apparently) imported on the main thread. However, this needs to be done on a per-install basis, so it's not very nice towards users of my Django app.

I tried catching the exception, but an import that raised an exception does not fully complete, so the next import async will fail as well.

Can you think of any halfway decent method to work/hack around this problem?


Update: I noticed that Apache's mod_wsgi is smart enough to ignore the signal call:

[Tue Sep 07 19:53:11 2010] [warn] mod_wsgi (pid=28595): Callback registration for signal 2 ignored.

The problem remains with the Django development server, though.


If you pull the latest async code from git, I suspect this will be fixed for you and is called out as a non-fatal error in the patch

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号