Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7566115
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T14:19:48+00:00 2026-05-30T14:19:48+00:00

I have a seemingly straight-forward situation, but can’t find a straight-forward solution. I’m using

  • 0

I have a seemingly straight-forward situation, but can’t find a straight-forward solution.

I’m using sqlalchemy to query postgres. If a client timeout occurs, I’d like to stop/cancel the long running postgres queries from another thread. The thread has access to the Session or Connection object.

At this point I’ve tried:

session.bind.raw_connection().close()

and

session.connection().close()

and

session.close

and

session.transaction.close()

But no matter what I try, the postgres query still continues until it’s end. I know this from watching pg in top. Shouldn’t this be fairly easy to do? I’m I missing something? Is this impossible without getting the pid and sending a stop signal directly?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T14:19:49+00:00Added an answer on May 30, 2026 at 2:19 pm

    This seems to work well, so far:

    def test_close_connection(self):
        import threading
        from psycopg2.extensions import QueryCanceledError
        from sqlalchemy.exc import DBAPIError
    
        session = Session()
        conn = session.connection()
        sql = self.get_raw_sql_for_long_query()
    
        seconds = 5
        t = threading.Timer(seconds, conn.connection.cancel)
        t.start()
    
        try:
            conn.execute(sql)
        except DBAPIError, e:
            if type(e.orig) == QueryCanceledError:
                print 'Long running query was cancelled.'
        t.cancel()
    

    source

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a seemingly simple question, but can't find the answer. I have a
I have a seemingly simple requirement, but i can't figure out how to write
I have a seemingly basic problem with C# arrays. I am a beginner, but
I have seemingly easy problem and am having trouble designing the right solution. I
I'm using POW and Rdebug, and I have it all seemingly working because when
I'm using Ruby's CSV library to parse some CSV. I have a seemingly well-formed
I have a seemingly corrupt h2 database. I can use org.h2.tools.Recover to get a
I'm using Entity Framework 4.1 and have a seemingly simple requirement: I want to
I have resisted asking this seemingly n00b question, but all my recent effort to
A seemingly simple question: Can I have some sort of Number object, which can

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.