Python 3.3 urllib memory leakage - memory

I'm using Mac mini with iOS 10.9.2 and Python 3.3.
I wrote a simple Python app, which fetches api data and shows some calculation.
Everything is working well, but I saw in Activity Monitor (os tool) that after a few connections the system memory usage increases, but in my opinion it shouldn't. I wouldn't publish application that can eat all memory so I need some help.
Here is a piece of my code which makes this problem:
import urllib.request
import time
class Main(object):
def Get(self, url):
urlData = urllib.request.urlopen(url)
for line in urlData:
line = str(line,'utf-8')
print( line.rstrip() )
urlData.close()
time.sleep(1)
M = Main()
url = "https://btc-e.com/api/2/btc_usd/trades"
b=1
while b>0:
M.Get(url)
b=b+1
I've tried to do it in another way but it increases the memory usage, too:
req = request.urlopen('https://btc-e.com/api/2/btc_usd/trades')
urlData = json.loads(req.read().decode('utf-8'))
This increases the memory usage, too:
from urllib.request import urlopen
html = urlopen("https://btc-e.com/api/2/btc_usd/trades")
Thank you in advance!

Related

Where is MobyLCPSolver?

Where is MobyLCPSolver?
ImportError: cannot import name 'MobyLCPSolver' from 'pydrake.all' (/home/docker/drake/drake-build/install/lib/python3.8/site-packages/pydrake/all.py)
I have the latest Drake and cannot import it.
Can anyone help?
As of pydrake v1.12.0, the MobyLcp C++ API is not bound in Python.
However, if you feed an LCP into Solve() then Drake can choose Moby to solve it. You can take advantage of this to create an instance of MobyLCP:
import numpy as np
from pydrake.all import (
ChooseBestSolver,
MakeSolver,
MathematicalProgram,
)
prog = MathematicalProgram()
x = prog.NewContinuousVariables(2)
prog.AddLinearComplementarityConstraint(np.eye(2), np.array([1, 2]), x)
moby_id = ChooseBestSolver(prog)
moby = MakeSolver(moby_id)
print(moby.SolverName())
# The output is: "Moby LCP".
# The C++ type of the `moby` object is drake::solvers::MobyLCP.
That only allows for calling Moby via the MathematicalProgram interface, however. To call any MobyLCP-specific C++ functions like SolveLcpFastRegularized, those would need to be added to the bindings code specifically before they could be used.
You can file a feature request on the Drake GitHub page when you need access to C++ classes or functions that aren't bound into Python yet, or even better you can open a pull request with the bindings that you need.

How to monitor Python Application(without django or other framework) in instana

i have a simple python application without any framework and API calls.
how i will monitor python application on instana kubernates.
i want code snippet to add with python application ,which trace application
and display on instana
how i will monitor python application on instana kubernates
There is publicly available guide, that should help you setting up the kubernetes agent.
i have a simple python application without any framework and API calls
Well, instana is for distributed tracing, meaning distributed components calling each other, each other's APIs predominantly by using known frameworks (with registered spans).
Nevertheless, you could make use of SDKSpan, here is a super simple example:
import os
os.environ["INSTANA_TEST"] = "true"
import instana
import opentracing.ext.tags as ext
from instana.singletons import get_tracer
from instana.util.traceutils import get_active_tracer
def foo():
tracer = get_active_tracer()
with tracer.start_active_span(
operation_name="foo_op",
child_of=tracer.active_span
) as foo_scope:
foo_scope.span.set_tag(ext.SPAN_KIND, "exit")
result = 20 + 1
foo_scope.span.set_tag("result", result)
return result
def main():
tracer = get_tracer()
with tracer.start_active_span(operation_name="main_op") as main_scope:
main_scope.span.set_tag(ext.SPAN_KIND, "entry")
answer = foo() + 21
main_scope.span.set_tag("answer", answer)
if __name__ == '__main__':
main()
spans = get_tracer().recorder.queued_spans()
print('\nRecorded Spans and their IDs:',
*[(index,
span.s,
span.data['sdk']['name'],
dict(span.data['sdk']['custom']['tags']),
) for index, span in enumerate(spans)],
sep='\n')
This should work in any environment, even without an agent and should give you an output like this:
Recorded Spans and their IDs:
(0, 'ab3af60079f3ca57', 'foo_op', {'span.kind': 'exit', 'result': 21})
(1, '53b67f7298684cb7', 'main_op', {'span.kind': 'entry', 'answer': 42})
Of course in a production, you wouldn't want to print the recorded spans, but send it to the well configured agent, so you should remove setting the INSTANA_TEST.

Capture ANSI escape sequence response using Micropython ESP8266

I'm using an ESP8266 with Micropython to communicate with a serial terminal using Putty. Using escape sequences like referenced in these questions:
Read ANSI escape from terminal
How do I determine size of ANSI terminal?
List of escape sequences https://vt100.net/docs/vt100-ug/chapter3.html
I am trying to read back an escape sequence that is a reply to find the cursor position.
I can see the response in the terminal, but I need to capture it so I can parse it. I'm familiar with Python, but a complete noob with Micropython and the ESP8266 (ESP01). I'm using the webrepl to upload the code. I saw this and I'm not sure if it's related: https://forum.micropython.org/viewtopic.php?t=5359
I've tried running my code without webrepl connected, but it still didn't work.
What am I doing wrong, how can I capture the response?
Depending on the terminal size, the response should be something like below, the stuff before the ; will not be visible.
^[[45;157R
#import esp
import network
import machine
from machine import UART
uart = UART(0, 115200)
x=uart.write("\033[500;500H\033[6n")
if uart.any():
print(":> ", uart.read())
EDIT: Tried example from this link https://forum.micropython.org/viewtopic.php?t=5359#p30867
No luck with that either (yes, I know it's blocking). Here if I type the letter 'R', it drops out of the loop, but it's not catching the output from the escape sequence.
from machine import UART
import machine
import network
import uos
uos.dupterm(None, 1)
uart = UART(0, 115200)
charbuf = None
uart.write("\033[500;500H\033[6n")
while charbuf != b"R":
if uart.any():
charbuf = uart.read()
uart.write(charbuf)
uos.dupterm(UART(0, 115200), 1)
Solving this was a nightmare, the WebRepl console replication has been the bane of my existence and probably many others. I tried multiple read() methods and they all failed.
uart.read()
sys.stdin.read()
sys.stdin.buffer.read()
Those were in combination of enabling/disabling the console duplication for webrepl.
The incantation that finally worked for me is below. See inline comments for more details.
import machine
import network
#import sys
import uos
from machine import UART
uos.dupterm(None, 1) # Disable console duplication for Webrepl
uart = UART(0, 115200, timeout=100, timeout_char=100) #Instantiate uart, increase timeout for good measure
uart.write("\033[2J\033[1;1f") # Clear screen
uart.write("\033[500;500H\033[6n") # Move cursor to bottom right, send query.
# Wait for uart to collect response. This will block further execution until we get a reply.
while True:
if uart.any() != 0:
break
uos.dupterm(UART(0, 115200), 1) # Re-enable console redirection so print() will work
# Read the uart buffer with response, convert from binary to string, replace ESC character.
# First ^ gets eaten somewhere!
buf = str(uart.read(), 'UTF-8').replace('\x1b', '^^[')
if len(buf) > 0: # If response isn't empty, print buffer
print(buf)
This is the Output:
^[[43;157R

IBM Cloud Functions - Python Actions

I'm learning how to use Serverless Functions, I'm working trying to connect a Watson assistant through webhooks using a python action that is processing a small dataset, I'm still struggling to succeed on it.
I’ve done my coding on Jupyter environment calling raw csv dataset from Github and using pandas to handle it. The issue is when I’m invoking the action into IBM Functions works 10% of the times. I did debug on Jupyter and Visual Studio environments and the code seems to be ok, but once I move the code to the IBM Functions environment it doesn't perform.
import sys
import csv
import json
import pandas as pd
location = ('Germany') #Passing country parameter for testing purpose
data = pd.read_csv('https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_daily_reports/03-24-2020.csv')
def main(args):
location = args.get("location")
for index, row in data.iterrows():
currentLoc = row['Country/Region']
if currentLoc == location:
covid_statistics = {
"Province/State": row['Province/State'],
"Country/Region": row['Country/Region'],
"Confirmed":row['Confirmed'],
"Deaths":row['Deaths'],
"Recovered":row['Recovered']
}
return {"message": covid_statistics}
else:
return {"message": "Data not available"}

Why does xLearn fit function causes kernel crashes in Jupyter?

I'm trying to make CTR (Click through rate) prediction using a python module named 'xlearn'.
It enables me to implement a FFM (field-aware factorisation machine) quite easily.
However, I have a problem with the fit function ( supposed to train the model) which crashes the kernel of my jupyter notebook without any error messages.
Here is the code :
import xlearn as xl
ffm_model = xl.create_ffm()
param = {'task':'binary', 'lr':0.2, 'lambda':0.002, 'metric':'acc'}
ffm_model.setTrain('ffm_train.txt')
ffm_model.fit(param, "./model.out") #this line crashes the kernel
I've already tried to fit the model just after python ffm_model = xl.create_ffm() this also crashes the kernel without any error messages ...
Don't hesitate to share your ideas I'm really stuck here.
I didn't realize the xLearn module was showing error messages in the terminal :
Xlearn Imgae Error Messages

Resources