builtins.ValueError: signal only works in main thread of the main interpreter

Hello!I am trying to build a basic streamlit dashboard style web app which will display and update live data from a stream. I am using the KiteConnect api for fetching stock prices each second and updating the app. I am getting the following Error Message when I run my streamlit app code:


Unhandled Error
Traceback (most recent call last):
  File "C:\Python\lib\site-packages\twisted\internet\base.py", line 498, in fireEvent
    DeferredList(beforeResults).addCallback(self._continueFiring)
  File "C:\Python\lib\site-packages\twisted\internet\defer.py", line 497, in addCallback
    return self.addCallbacks(callback, callbackArgs=args, callbackKeywords=kwargs)
  File "C:\Python\lib\site-packages\twisted\internet\defer.py", line 477, in addCallbacks
    self._runCallbacks()
  File "C:\Python\lib\site-packages\twisted\internet\defer.py", line 857, in _runCallbacks
    current.result = callback(  # type: ignore[misc]
--- <exception caught here> ---
  File "C:\Python\lib\site-packages\twisted\internet\base.py", line 510, in _continueFiring
    callable(*args, **kwargs)
  File "C:\Python\lib\site-packages\twisted\internet\base.py", line 1311, in _reallyStartRunning
    self._handleSignals()
  File "C:\Python\lib\site-packages\twisted\internet\posixbase.py", line 338, in _handleSignals
    _SignalReactorMixin._handleSignals(self)
  File "C:\Python\lib\site-packages\twisted\internet\base.py", line 1279, in _handleSignals
    signal.signal(signal.SIGTERM, reactorBaseSelf.sigTerm)
  File "C:\Python\lib\signal.py", line 47, in signal
    handler = _signal.signal(_enum_to_int(signalnum), _enum_to_int(handler))
builtins.ValueError: signal only works in main thread of the main interpreter

Following is the section of code which subscribes to the live stream and updates the web app components:

with open('api_config.txt','r') as f:
     info=json.load(f)
 # Initialise
 kws = KiteTicker(info['api_key'], info['access_token'])
 # Assign the callbacks.
 kws.on_ticks = on_ticks
 kws.on_connect = on_connect
 kws.on_close = on_close
 # Infinite loop on the main thread. Nothing after this will run.
 # You have to use the pre-defined callbacks to manage subscriptions.
 kws.connect()

def on_ticks(ws, ticks):
 # Callback to receive ticks.
 #logging.debug("Ticks: {}".format(ticks))
 # 0 VIX 264969
 # 1 NIFTY 50 256265
 
 #Updating variables with values obtained from feed
 #vix_open,vix_high,vix_low,vix_close,vix_pclose,vix_perchange,nif_ochange

 #Replacing components in every iteration
 with placeholder.container():
     k_vix_open,k_vix_high,k_vix_low,k_vix_close,k_vix_pclose,k_vix_perchange,k_nif_ochange=st.columns(7)
     k_vix_open.metric(
         label='Open',
         value=vix_open,
         delta=np.sign(vix_open)
         
     )
     k_vix_high.metric(
         label='High',
         value=vix_high,
         delta=np.sign(vix_high)

     )
     k_vix_low.metric(
         label='Low',
         value=vix_low,
         delta=np.sign(vix_low)

     )
     k_vix_close.metric(
         label='Close',
         value=vix_close,
         delta=np.sign(vix_close)
     )
     k_vix_pclose.metric(
         label='Close',
         value=vix_pclose,
         delta=np.sign(vix_pclose)
     )
     k_vix_perchange.metric(
         label='% Change VIX',
         value=vix_perchange,
         delta=np.sign(vix_perchange)
     )
     k_nif_ochange.metric(
         label='% Overnight Change Nifty',
         value=nif_ochange,
         delta=np.sign(nif_ochange)
     )

I would appreciate any inputs for this. Thank you!

Hello, I am facing the same error. Did you find any solution to it?
This is the error message I am getting:

File "/home/folder/check_env/lib/python3.11/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 552, in _run_script
    exec(code, module.__dict__)
File "/home/folder/stremlitapp.py", line 130, in <module>
    article, title = extract_text_from_url(URL)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/folder/stremlitapp.py", line 77, in extract_text_from_url
    tra_article = trafilatura.extract(downloaded)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/folder/check_env/lib/python3.11/site-packages/trafilatura/core.py", line 1054, in extract
    signal(SIGALRM, timeout_handler)
File "/home/anaconda3/lib/python3.11/signal.py", line 56, in signal
    handler = _signal.signal(_enum_to_int(signalnum), _enum_to_int(handler))
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

The peculiar thing is that the error occurs when executing the line: article, title = extract_text_from_url(URL), which - when executing it without streamlit - works out fine. How can I fix this issue?

Thanks!

I have the same issue, also using trafilatura

Hey @mih, I don’t know if this helps in your case, but i was able to fix the problem by using a different library to extract the text. Here is the corresponding thread: Facing ValueError: signal only works in main thread - #4 by pauls33