question

Upvote
Accepted
38 0 0 0

Connection to streaming news works in jupyter notebook but not in python script

Hello,


I am currently trying to retrieve streaming news for RDP platform.


When I run the following code in a jupyter notebook cell all is working fine :


import refinitiv.dataplatform as rdp

import datetime

import zlib

import base64

import json

APP_KEY = '***'

MACHINE_ID = '***'

PASSWD = '***'


rdp.open_platform_session(app_key=APP_KEY, 
                          grant=rdp.GrantPassword(username=MACHINE_ID, 
                                                  password=PASSWD)
                          )
 
item_stream = rdp.ItemStream(session=rdp.get_default_session(),
                             domain=DOMAIN,
                             name=NAME, #should be optional
on_refresh = lambda item_stream, event : processMRNstart("Refresh", event),
on_update = lambda item_stream, event : processMRNUpdate("Update", event),
on_status = lambda item_stream, event : processMRNstart("Status", event),
on_complete = lambda item_stream : processMRNstart("Complete", "")
                            )
item_stream.open()


However, when I copy paste this code into a python script .py and run in into the command shell it stucks around the rpd.open_platform_session function and nothing happens.


Any ideas about the different behavior between Jupyter notebook and Python scripts for getteting streaming news from RDP platform ?



rdp-apirefinitiv-data-platformwebsocketsrefinitiv-data-platform-libraries
icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 5.0 MiB each and 10.0 MiB total.

Upvote
Accepted
23k 22 9 14

Hi @simon.corde-e,

I suspect what is happening. Try the following, let us know how this works for you:

import refinitiv.dataplatform as rdp
import datetime
import zlib
import base64
import json
import time

app_key = 'YOURKEY'
user = 'YOURUSER'
password = 'YOURPASSWORD'

rdp.open_platform_session(app_key=app_key,
     grant=rdp.GrantPassword(username=user,
     password=password)
     )
item_stream = rdp.ItemStream(session = rdp.get_default_session(),
     name = "EUR=",
     on_refresh = lambda s, msg : print(json.dumps(msg, indent=2)),
     on_update = lambda s, msg : print(json.dumps(msg, indent=2)),
     on_status = lambda s, msg : print(json.dumps(msg, indent=2)))

item_stream.open()
while True: time.sleep(0.2) 
icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 5.0 MiB each and 10.0 MiB total.

Thank you for the tip. Indeed, it works now when I run python3 myscript.py !

Upvotes
23k 22 9 14

Hello @simon.corde-e,

For me this code runs the same from jupyter notebook and from python script in command shell.

Are you finding the same version of python, with the same versions of the libraries installed, via jupiter python as you do via command shell?

To confirm, run from pybook and from shell:

python --version
icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 5.0 MiB each and 10.0 MiB total.

Upvote
11.5k 16 7 10

Hello @simon.corde-e

I have tried the code with both Python Console and Jupyter Notebook. They can run and give the same result.

Did you run Notebook and Console on the same Python environment as suggest by my colleague above?

Beside the Python version, you also need to same libraries (and versions) as well. You can use the following commands to list the installed libraries

  • Conda/Mini Conda: conda list
  • Python Pip: pip list
icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 5.0 MiB each and 10.0 MiB total.

Upvotes
38 0 0 0

Thank you for your quick answer.

In fact, I use exactly the same environment between shell and jupyter notebook (same python version and same libraries versions).

I switch from a windows environement in which I launched my python script from the powershell to a linux environment and the connexion now works (the status is displayed as open). However, in the python script I print and write the incoming streaming news. In jupyter notebook it is ok but from the shell (I run the following command python3 myscript.py) there is no print in the shell console nor anything written in the output file.


I have instanciated a logger to see what is happenning and it appears we get the status event ('status': <StreamState.Open: 3>) then the refresh event but the logs stop afterwards.

icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 5.0 MiB each and 10.0 MiB total.

The command python3 -i myscript.py allows to run the script and open a python command prompt. Is it an expected behavior that getting the streaming news from a Jupyter Notebook or a python command prompt works but not when run from the console as python3 myscript.py ? Indeed, we intend to run the extraction of the news in the background (we would like to run it 24/24H on a linux server)

Upvote
11.5k 16 7 10

Hello @simon.corde-e

Could you please also give us the full source code of your Python script (especially the processMRNstart and processMRNupdate functions source code)?


icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 5.0 MiB each and 10.0 MiB total.

Click below to post an Idea Post Idea