Skip to content

How to make simultaneous Slack API calls without reinitializing the client? #497

@hbandlamudi

Description

@hbandlamudi
  • bug
  • enhancement (feature request)
  • question
  • documentation related
  • testing related
  • discussion
  • [x ] I've read and understood the Contributing guidelines and have done my best effort to follow them.
  • [x ] I've read and agree to the Code of Conduct.
  • [x ] I've searched for any related issues and avoided creating a duplicate issue.

Reproducible in:

slackclient version: 2.1.0

python version: 3.7

I created a Slack app (using Flask) using version 1.3.1 and it could handle simultaneous Slack API calls without any issue. Since then, I've tried upgrading to slackclient==2.1.0 and see that when simultaneous calls are made, one of them will fail with the a RunTimeError that the This event loop is already running.

In order to further investigate, I created a simple Flask app which can handle simultaneous calls. The code below works without any issue however if I try to move the loop and slack WebClient initialization outside of the entry() method, simultaneous calls fail. Do these variables need to be reinitialized on each request or is there another way to only initialize them once yet be able to handle concurrent requests?

from flask_api import status
import asyncio
import slack

app = Flask(__name__)
bot_slack_token = 'xoxb-xxxxxxxxxx'

response_headers = {
    'Content-Type': 'application/json'
}

async def send_async_message(channel, text, client):
    response = await client.chat_postMessage(channel=channel, text=text)

    assert response["ok"]
    assert response["message"]["text"] == "ok"


@app.route("/")
def entry():
    asyncio.set_event_loop(asyncio.new_event_loop())
    loop = asyncio.get_event_loop()

    client = slack.WebClient(
        token=bot_slack_token,
        run_async=True
    )

    loop.run_until_complete(send_async_message(channel='#hello', text="ok", client=client)) 

    return make_response("", status.HTTP_200_OK, response_headers)


if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0')



Metadata

Metadata

Assignees

Labels

Version: 2xarea:concurrencyIssues and PRs related to concurrencybugM-T: A confirmed bug report. Issues are confirmed when the reproduction steps are documentedquestionM-T: User needs support to use the projectrtm-clientweb-client

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions