Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.0k views
in Technique[技术] by (71.8m points)

python - combined client and server using aiohttp

I am trying to find an example of a python aiohttp app that both makes http requests as a client AND processes http requests as a server.

I need to write an app that retrieves data from an internet web server AND responds to requests for the re-formatted and re-packaged data.

Could someone reference an example or provide an outline of how to structure such an app using aiohttp?

One issue I am having trouble figuring out is how to combine the loop.run_until_complete() used to start a client that is making requests and web.run_app() that is used to start a server since each of these is a blocking call.

Somehow I need to start them both.

I thought web.AppRunner() might help me start the server in a non-blocking manner, and I came across the following example in the aiohttp documentation:

runner = web.AppRunner(app)
await runner.setup()
site = web.TCPSite(runner, 'localhost', 8080)
await site.start()

while True:
    await asyncio.sleep(3600)  # sleep forever

I thought I could use something like this to start the server and then also start the client. But I can't seem to call this from anything that isn't itself an async function (await outside async function).

I could also use some advice on how to share the data between the client process and the server process as the client need to periodically refresh the data from the internet and the server needs to be able to continuously serve up the latest version of the data. Presumably I need some sort of locking mechanism while the client is updating the data resource shared between the client process and server process?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share

2.1m questions

2.1m answers

62 comments

56.6k users

...