Remove Stream.new_session
authorHarmon <Harmon758@gmail.com>
Fri, 22 Jan 2021 23:53:55 +0000 (17:53 -0600)
committerHarmon <Harmon758@gmail.com>
Fri, 22 Jan 2021 23:54:43 +0000 (17:54 -0600)
Stop unnecessarily creating a new requests Session at the end of each call to Stream._run

tweepy/streaming.py

index a664cd4784cd2c0f4d5f411b245c8520d99dcbd0..5323cba0d9180153a9275db3a386703968b6b190 100644 (file)
@@ -144,18 +144,17 @@ class Stream:
 
         self.verify = options.get("verify", True)
 
-        self.new_session()
+        self.session = None
         self.retry_time = self.retry_time_start
         self.snooze_time = self.snooze_time_step
 
         # Example: proxies = {'http': 'http://localhost:1080', 'https': 'http://localhost:1080'}
         self.proxies = options.get("proxies")
 
-    def new_session(self):
-        self.session = requests.Session()
-
     def _run(self, params=None, body=None):
         # Authenticate
+        if self.session is None:
+            self.session = requests.Session()
         url = f"https://stream.twitter.com{self.url}"
 
         # Connect and process the stream
@@ -212,7 +211,6 @@ class Stream:
             raise
         finally:
             self.running = False
-            self.new_session()
 
     def _read_loop(self, resp):
         for line in resp.iter_lines(chunk_size=self.chunk_size):