0

I'm doing a project for class where I stream data from Twitter using Databricks and when it reaches s.accept() it seems to get stuck there, running indefinitely:

Code:

def sendTweets(c_socket):

 auth = OAuthHandler(API_key, API_secret_key)
 auth.set_access_token(access_token, access_token_secret)
 stream = Stream(auth, getTweets(c_socket))
 stream.filter(track=['covid','covid-19','covid19','coronavirus']) 

s = socket.socket()
host = "127.0.0.1"
port = 3333
s.bind((host, port))
s.listen()
c, addr = s.accept()
sendTweets(c)

Sorry if it's a very dumb question

javverde
  • 3
  • 2
  • Please refrain from putting your code in a screenshot. https://idownvotedbecau.se/imageofcode If you edit the question and paste the code into the question and indent it by 4 spaces so we can see it without images, let me know. I'll be glad to reverse my downvote. :D – TheTechRobo the Nerd Apr 04 '21 at 00:55
  • 1
    Like this? Sorry, first time here :( – javverde Apr 04 '21 at 01:01
  • Yeah, you got most of it. :) Just indent the parts that are not indented by 4 spaces. It's ok that you didn't know btw - we're all new at some point. :) My first question on the site was about playing a game because a google search of an error with unity web player brought me to this site! :P – TheTechRobo the Nerd Apr 04 '21 at 01:02
  • 1
    There :) Thanks! – javverde Apr 04 '21 at 01:03
  • Yep, that's perfect. It's just much nicer to read. :) – TheTechRobo the Nerd Apr 04 '21 at 01:03
  • Which are your imports? I don't use databricks, so I have no idea :P – TheTechRobo the Nerd Apr 04 '21 at 01:05
  • 1
    This is what I have (Also if my total ignorance wasn't clear, this is about my first time with Python, we don't use it in my university except for some optional subjects): import tweepy, from tweepy import OAuthHandler, from tweepy import Stream, from tweepy.streaming import StreamListener, import socket, import json – javverde Apr 04 '21 at 01:08
  • Does this answer your question? [socket.accept() hangs my program](https://stackoverflow.com/questions/21750606/socket-accept-hangs-my-program) – TheTechRobo the Nerd Apr 04 '21 at 01:09
  • 1
    For what I have read it hangs until a client connects, but...apparently no client is connecting – javverde Apr 04 '21 at 01:22

1 Answers1

0

This answer is untested and probably won't work but hopefully it'll give you an idea.

You could try the following (not my best code):

import threading
def sendTweets(c_socket):

 auth = OAuthHandler(API_key, API_secret_key)
 auth.set_access_token(access_token, access_token_secret)
 stream = Stream(auth, getTweets(c_socket))
 stream.filter(track=['covid','covid-19','covid19','coronavirus']) 

def StartSocket():
    global c, addr
    c, addr = s.accept()

s = socket.socket()
host = "127.0.0.1"
port = 3333
s.bind((host, port))
s.listen()
thread = threading.Thread(target=StartSocket) #fork off the server code
thread.start()
sendTweets(c)
TheTechRobo the Nerd
  • 1,249
  • 15
  • 28