johnowhitaker commited on
Commit
f4db971
1 Parent(s): dc2ae70

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -0
app.py CHANGED
@@ -3,7 +3,29 @@ import pandas as pd
3
  import twint
4
  import nest_asyncio
5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
  # Function to make this easy for ourselves:
 
 
7
  def get_tweets(username, limit=500, save_name=None):
8
  #nest_asyncio.apply() # Helps avoid RuntimeError: This event loop is already running
9
 
 
3
  import twint
4
  import nest_asyncio
5
 
6
+ import multiprocessing.pool
7
+ import functools
8
+
9
+ # https://stackoverflow.com/questions/492519/timeout-on-a-function-call
10
+ def timeout(max_timeout):
11
+ """Timeout decorator, parameter in seconds."""
12
+ def timeout_decorator(item):
13
+ """Wrap the original function."""
14
+ @functools.wraps(item)
15
+ def func_wrapper(*args, **kwargs):
16
+ """Closure for function."""
17
+ pool = multiprocessing.pool.ThreadPool(processes=1)
18
+ async_result = pool.apply_async(item, args, kwargs)
19
+ # raises a TimeoutError if execution exceeds max_timeout
20
+ return async_result.get(max_timeout)
21
+ return func_wrapper
22
+ return timeout_decorator
23
+
24
+ nest_asyncio.apply()
25
+
26
  # Function to make this easy for ourselves:
27
+ @st.cache
28
+ @timeout(5.0)
29
  def get_tweets(username, limit=500, save_name=None):
30
  #nest_asyncio.apply() # Helps avoid RuntimeError: This event loop is already running
31