I'm trying to run this code in parallel:
availablePrefix = {"http://URL-to-somewehere.com": "true", "http://URL-to-somewehere-else.com": "true"}
def main():
while True:
prefixUrl = getFreePrefix() # Waits until new url is free
sendRequest("https://stackoverflow.com/", prefixUrl)
def getFreePrefix():
while True:
for prefix in self.availablePrefix.keys():
if availablePrefix.get(prefix) == "true":
availablePrefix[prefix] = "false" # Can't be used for another request
return prefix
async def sendRequest(self, prefix, suffix):
url = prefix + "/" + suffix
async with aiohttp.ClientSession() as session:
async with session.get(url) as resp:
response = await resp.text()
availablePrefix[prefix] = "true" # Can be used again
return json.loads(response)
Basically, I'm trying to run the main() function in parallel. The main() function is stuck until getFreePrefix() returns a new prefix (URL to my server). With the help of this prefix we can access my server and start a request. If this Prefix is used, it is set to false, to indicate that it can't be used for another request right now (If request is completed, it is set to true again).
What I want to achieve is, that every time a new prefix is ready, a new request is run in parallel.
Thanks for helping!