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

RuntimeError: set_wakeup_fd only works in main thread #2265

Closed
eteamin opened this issue Sep 12, 2017 · 3 comments
Closed

RuntimeError: set_wakeup_fd only works in main thread #2265

eteamin opened this issue Sep 12, 2017 · 3 comments
Labels

Comments

@eteamin
Copy link
Contributor

eteamin commented Sep 12, 2017

Long story short

I'm trying to run an aiohttp Application in a deamon thread. I use asyncio.get_child_watcher() in the main thread so subprocesses will be allowed to run. but I get RuntimeError: set_wakeup_fd only works in main thread

Expected behaviour

Running aiohttp Application in a worker thread

Actual behaviour

RuntimeError is raised

Steps to reproduce

A little more of context:
I'm writing tests for an aiohttp application. This app calls an external rest-full API to get some data. Now I'm trying to create another aiohttp application in a daemon thread to mock that third-party application behaviour.
This is my MockServer class:

class MockingServer(object):
    def __init__(self, host, port):
    self.host = host
    self.port = port
    self.app = Application()
    r = Resource('/validate_get/{content_uid}/{user_uuid}', Route('GET', '/validate_get', self.mock_handler))
    resource = self.app.router.add_resource(r.path)
    resource.add_route(r.route.method, r.route.handler)

    async def mock_handler(self, request):
        return json_response({'ok': True})

    def start(self):
        asyncio.get_child_watcher()
        thread = Thread(target=self._start, daemon=True)
        thread.start()

    def _start(self, *args):
        loop = asyncio.new_event_loop()
        asyncio.set_event_loop(loop)
        run_app(self.app, port=self.port, loop=loop)` 

Your environment

Python 3.5.2
OSX
aiohttp

@asvetlov
Copy link
Member

asvetlov commented Sep 12, 2017

You don't need a separate thread for running mocking server.
Take a look on https://github.com/aio-libs/aiohttp/blob/master/examples/fake_server.py

@eteamin
Copy link
Contributor Author

eteamin commented Sep 12, 2017

Thank you for the useful example!

@lock
Copy link

lock bot commented Oct 28, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a [new issue] for related bugs.
If you feel like there's important points made in this discussion, please include those exceprts into that [new issue].
[new issue]: https://github.com/aio-libs/aiohttp/issues/new

@lock lock bot added the outdated label Oct 28, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Oct 28, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants