alexandrebarachant / muse-lsl

Python script to stream EEG data from the muse 2016 headset
BSD 3-Clause "New" or "Revised" License
629 stars 184 forks source link

Problem with stream when started in separated thread. #202

Open seemingly-ai opened 1 year ago

seemingly-ai commented 1 year ago

Hello, I tried writing a script that would start the streaming of eeg, ppg, acc and gyro from an Muse S Gen 2 and then view and/or record it. But I ran into a little Problem. As soon as I try to start the stream in a separated thread using the threading module I keep getting error massages that seem to indicat a asyncronous subroutine not beeing handled correctly. But I don´t understand why as the stream works perfectly fine while not beeing started in a separated thread.

What was working:

  1. Starting the stream in a terminal with "muselsl stream --ppg --acc --gyro" and then running "muselsl view" in a different terminal.
  2. Starting the stream via a python script and then running "muselsl view" in a different terminal.
  3. Starting the stream in a terminal with "muselsl stream --ppg --acc --gyro" and then executing the "view()" function via a python script.

But like I said, as soon as the stream is beeing handled in a different thread nothing seem to work anymore. This is the script:


import logging
from threading import Thread
from muselsl import list_muses, stream, view

def stream_thread(address):
    stream(address, ppg_enabled=True, acc_enabled=True, gyro_enabled=True)
    logging.info("Stream has ended")

def main():
    format = "%(asctime)s: %(message)s"
    logging.basicConfig(format=format, level=logging.INFO, datefmt="%H:%M:%S")

    muses = list_muses()
    address = muses[0]['address']

    stream = Thread(target=stream_thread, args=(address,))
    stream.start()

if __name__ == "__main__":
    main()

And here is the output with the error massage:


Searching for Muses, this may take up to 10 seconds...
Found device MuseS-7FC9, MAC Address [...]
2023-10-04 22:34:43.782 (  11.960s) [         3D96734]      netinterfaces.cpp:36    INFO| netif '{9DCBAEAF-F36A-4838-B7B2-39F75739C2C6}' (status: 2, multicast: 1
2023-10-04 22:34:43.783 (  11.960s) [         3D96734]      netinterfaces.cpp:36    INFO| netif '{61939EE2-C0FF-459B-A023-433C0013E4CD}' (status: 2, multicast: 1
2023-10-04 22:34:43.783 (  11.960s) [         3D96734]      netinterfaces.cpp:36    INFO| netif '{08C331E9-74A0-42CB-A401-3825B09DC210}' (status: 1, multicast: 1
2023-10-04 22:34:43.783 (  11.960s) [         3D96734]      netinterfaces.cpp:58    INFO|       IPv6 ifindex 4
2023-10-04 22:34:43.783 (  11.960s) [         3D96734]      netinterfaces.cpp:36    INFO| netif '{4310C865-2407-4C33-8D2B-ED2F679ABB2A}' (status: 2, multicast: 1
2023-10-04 22:34:43.783 (  11.961s) [         3D96734]      netinterfaces.cpp:36    INFO| netif '{D45D6B56-F09B-11E9-AA57-806E6F6E6963}' (status: 1, multicast: 1
2023-10-04 22:34:43.783 (  11.961s) [         3D96734]      netinterfaces.cpp:58    INFO|       IPv6 ifindex 1
2023-10-04 22:34:43.783 (  11.961s) [         3D96734]         api_config.cpp:270   INFO| Loaded default config
2023-10-04 22:34:43.792 (  11.969s) [         3D96734]             common.cpp:65    INFO| git:6dc417089a1f73f26589b4f1e64def357b183e9c/branch:refs/tags/v1.16.1/build:Release/compiler:MSVC-19.0.24245.0/link:SHARED
2023-10-04 22:34:43.794 (  11.971s) [         3D96734]         udp_server.cpp:82    WARN| Could not bind multicast responder for ff02:113d:6fdd:2c17:a643:ffe2:1bd1:3cd2 to interface ::1 (Ein ung³ltiges Argument wurde angegeben.)
2023-10-04 22:34:43.796 (  11.973s) [         3D96734]         udp_server.cpp:82    WARN| Could not bind multicast responder for ff05:113d:6fdd:2c17:a643:ffe2:1bd1:3cd2 to interface ::1 (Ein ung³ltiges Argument wurde angegeben.)
2023-10-04 22:34:43.808 (  11.985s) [         3D96734]         udp_server.cpp:82    WARN| Could not bind multicast responder for ff02:113d:6fdd:2c17:a643:ffe2:1bd1:3cd2 to interface ::1 (Ein ung³ltiges Argument wurde angegeben.)
2023-10-04 22:34:43.808 (  11.986s) [         3D96734]         udp_server.cpp:82    WARN| Could not bind multicast responder for ff05:113d:6fdd:2c17:a643:ffe2:1bd1:3cd2 to interface ::1 (Ein ung³ltiges Argument wurde angegeben.)
2023-10-04 22:34:43.813 (  11.991s) [         3D96734]         udp_server.cpp:82    WARN| Could not bind multicast responder for ff02:113d:6fdd:2c17:a643:ffe2:1bd1:3cd2 to interface ::1 (Ein ung³ltiges Argument wurde angegeben.)
2023-10-04 22:34:43.814 (  11.991s) [         3D96734]         udp_server.cpp:82    WARN| Could not bind multicast responder for ff05:113d:6fdd:2c17:a643:ffe2:1bd1:3cd2 to interface ::1 (Ein ung³ltiges Argument wurde angegeben.)
2023-10-04 22:34:43.816 (  11.994s) [         3D96734]         udp_server.cpp:82    WARN| Could not bind multicast responder for ff02:113d:6fdd:2c17:a643:ffe2:1bd1:3cd2 to interface ::1 (Ein ung³ltiges Argument wurde angegeben.)
2023-10-04 22:34:43.817 (  11.994s) [         3D96734]         udp_server.cpp:82    WARN| Could not bind multicast responder for ff05:113d:6fdd:2c17:a643:ffe2:1bd1:3cd2 to interface ::1 (Ein ung³ltiges Argument wurde angegeben.)
Connecting to Muse: 00:55:DA:B9:7F:C9...
Exception in thread Thread-1 (stream_thread):
Traceback (most recent call last):
  File "[...]\Python311\Lib\threading.py", line 1038, in _bootstrap_inner
    self.run()
  File "[...]\Python311\Lib\threading.py", line 975, in run
    self._target(*self._args, **self._kwargs)
  File "[...]\simple_script.py", line 6, in stream_thread
    stream(address, ppg_enabled=True, acc_enabled=True, gyro_enabled=True)
  File "[...]\Python311\Lib\site-packages\muselsl\stream.py", line 221, in stream
    didConnect = muse.connect()
                 ^^^^^^^^^^^^^^
  File "[...]\Python311\Lib\site-packages\muselsl\muse.py", line 84, in connect
    self.device = self.adapter.connect(self.address)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "[...]\Python311\Lib\site-packages\muselsl\backends.py", line 37, in connect
    result.connect()
  File "[...]\Python311\Lib\site-packages\muselsl\backends.py", line 45, in connect
    _wait(self._client.connect())
  File "[...]\Python311\Lib\site-packages\muselsl\backends.py", line 10, in _wait
    loop = asyncio.get_event_loop()
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "[...]\Python311\Lib\asyncio\events.py", line 677, in get_event_loop
    raise RuntimeError('There is no current event loop in thread %r.'
RuntimeError: There is no current event loop in thread 'Thread-1 (stream_thread)'.
[...]\Python311\Lib\threading.py:1040: RuntimeWarning: coroutine 'BleakClient.connect' was never awaited
  self._invoke_excepthook(self)
RuntimeWarning: Enable tracemalloc to get the object allocation traceback

I´m using python Python 3.11.2 on Window 11