python flask socket send call never returns

I wrote a flask view function that send a query to a local service via sockets and then processes the data and returns it after rendering.

@app.route('/_search', methods=["GET", "POST"]) @app.route('/_search/', methods=["GET", "POST"]) def searchQuery(): # get the argument that were passed using get t_query = request.args.get('query') print(t_query) t_s = get_searchserver_socket() t_s.send("C, something") t_data = t_s.recv(1024) print(t_data) return t_data #abort(404)

I just use the global variable in flask to hold the socket so it doesn't recreated it everytime:

def get_searchserver_socket(): t_socket = getattr(g, '_socket', None) if t_socket is None: t_socket = g._socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) t_socket.connect((search_server_ip, search_server_port)) return t_socket

the problem with this is that in the view function, the socket send call (t_s.send) gets stuck and actually never sends the data. Why is this?


-------------Problems Reply------------

flask.g is only available for the current request. It exists in the application context, which you can read more about here:

You still will be re-creating this socket each time. If you want it to be persistent you'll have to create the socket outside of the Flask application context (outside of your Flask app) and then use it from there.

Category:python Views:1 Time:2017-11-07

Related post

  • erlang socket send timeout never happens 2010-12-01

    I am implementing a TCP Server in Erlang which talks to mobile phone clients. Mobile phones get offline a lot, so the server must be able to detect it. Hence I want the server to send messages to clients with a timeout, so that when the timeout happe

  • Hang-up on TCP socket send call in Android native code 2011-11-02

    I have a problem with socket send (or write) function on android. There is my network lib that I use on Linux and Android. Code is written in C. On Android, application creates a service, which loads a native code and creates the connection with the

  • What happens if an asynchronous delegate call never returns? 2010-06-08

    I found a decent looking example of how to call a delegate asynchronously with a timeout... In summary it uses WaitOne with a timeout to d

  • python blocking sockets, send returns immediately 2010-03-12

    I am writing a multithreaded socket application in Python using the socket module. the server listens for connections and when it gets one it spawns a thread for that socket. the server thread sends some data to the client. but the client is not yet

  • Python : 2 sockets, sending from A to B and from B to A 2011-11-24

    I've been working on this for 2 days and still I do not manage to get it to work properly. I wanted to write an application that uses 2 sockets with a medium in the middle This medium is this script that should read from socketA and write to SocketB

  • Increasing the amount of data sent in a single socket.send() call 2012-01-17

    We have code similar to the one below to create an SSL enabled socket between a machine and a remote host to transfer data. We notice that the amount of data sent in each call to sock.send is about 16K. Is there a way to configure this? Send 10 MB of

  • Java paypal call never returns result 2011-03-08

    Java I am using paypal website pro sdk SOAP calls problem is following line of code returns exception after long time response = (CreateRecurringPaymentsProfileResponseType)"CreateRecurringPaymentsProfile", request); Exception is as foll

  • ASP.NET MVC 4 asyncronous database call: never returns the HTTP response 2012-04-02

    I tried to make an async database call to SQL Server and use it along with new ASP.NET MVC 4 async features but it strangely does not return at all. I debugged the code, works nicely but somehow the HTTP request hangs open forever. Here is what I did

  • isValid() call never returns on MySQL JDBC Connection 2009-08-04

    I have trying to check whether connection is valid or not and using isValid() method of java.sql.Connection. But this method doesn't return and hangs. Is there anything I am missing or this method requires any configuration? Using mysql-connector-jav

  • Method call on remote object never returns (.NET Remoting) 2011-03-11

    The problem arises after I restart the remoting server. Client, server register their channels successfully Client successfully calls method on remote object I manually restart the server Client calls method again, but this time the method call never

  • RunStandardAlert never returns, buttons are non-responsive 2010-05-27

    I'm trying to create a simple dialog box from within a very simple application. It's the only UI in the application. But when I call RunStandardAlert, the buttons are non-responsive, and the function call never returns. I am not using Carbon or Cocoa

  • Problem with simple Ajax/REST client and XML response (never returns) 2011-06-26

    We have a web application where we can, from a browser, do an HTTP GET, and it returns an XML document. That works fine. To test this web app, I wrote a simple Java app using Jersey and JAX-RS, and that also works fine, i.e., I send in a GET request,

  • YAML.load never returns in Rails initializer 2012-02-20

    I'm trying to load a yaml config file during initialization of my Rails 3.1 app, and the call to YAML.load never returns. Here is my initializer file: STRIPE_CONFIG = begin config = YAML.load(Rails.root.join('config', 'stripe.yml')) || {} config = co

  • Non-blocking socket on Windows doesn't return after send() call 2009-11-15

    I'm hoping someone can explain a situation -- any situation -- in which a non-blocking Windows socket would not immediately return after using send() on it. On other platforms, my code works as intended, so it appears to be a Windows-specific issue.

  • Silverlight Socket.ConnectAsync method never calls the Completed event 2010-11-03

    Silverlight's Socket.ConnectAsync method never calls the Completed event - I am using . I have configured IIS to serve clientaccesspolicy.xml file on port 943. I am using following code: SocketAsyncEventArgs args = new SocketAsyncEvent

  • unix sockets: how to send really big data with one "send" call? 2009-10-16

    I'm using unix scoket for data transferring (SOCK_STREAM mode) I need to send a string of more than 100k chars. Firstly, I send length of a string - it's sizeof(int) bytes. length = strlen(s) send(sd, length, sizeof(int)) Then I send the whole string

  • python: can't terminate a thread hung in socket.recvfrom() call 2010-04-06

    I cannot get a way to terminate a thread that is hung in a socket.recvfrom() call. For example, ctrl+c that should trigger KeyboardInterrupt exception can't be caught. Here is a script I've used for testing: from socket import * from threading import

  • how to call a program from python without waiting for it to return 2010-04-08

    is there a way to call a program from python without waiting for it to return? i created a script which copies a program to a directory and runs that program. but when i call the program from python, the python script does not exit until the program

  • Python socket.send() is not sent until socket is closed 2011-02-01

    I'm building an IRC bot in Python for fun. It is supposed to accept commands prefixed with '!' and act on them. The function below is used to parse commands received in an IRC message. def parse_cmd(self, sender): #Admin Commands if sender == self.ow

  • Delphi.NET Socket BeginSend callback never get called 2011-04-04

    Recently, i found my application, which's developped under Delphi.NET, the socket callback function never get called, causing the socket message cannot be sent to the target client. Is it possible a bug of the socket class in Delphi.NET? Or will it b

  • Calls to Socket.send in little time causes two differente data to be sent in same packet 2011-05-19

    I'm programming a game which sends the position of the player to the server whenever the player moves, using XML like this: <?xml version="1.0" encoding="utf-8" ?> <request type="movement"> <character map="1" coords="10,20"/> </r

  • How to properly send HTTP response with Python using socket library only? 2012-04-11

    I have a very simple web sever written in Python. It listens on port 13000, how can I make it deliver a simple "Hello World" webpage if http://localhost:13000 is opened in browser? Right there is my code: # set up socket and connection while True: so

  • Preventing grouping of Python socket.send data from Queue 2010-03-02

    I am currently making a network layer built that creates binary messages and then sends them to the appropriate network interface (this could be Ethernet, WiFi, Bluetooth, etc). In working on the TCP module, I'm using Python's socket module and every

  • Can socket send / recv return errno 27 (EFBIG) on Solaris? 2010-09-21

    Can socket send / recv set errno 27 (EFBIG) on Solaris? Under which condition this happens? --------------Solutions------------- man recv doesn't document EFBIG meaning that if you see it, you have encountered a bug in the OS. Contact the Sun Oracle

Copyright (C), All Rights Reserved.

processed in 0.084 (s). 11 q(s)