ray.wait() on multiple functions with different completion time - ray

I am struggling with processing results of two tasks on completion using Ray 2.2. Below is my code. What's wrong here?
task_1 = A.remote(a1,a2)
task_2 = B.remote(b1,b2)
result_ids = [task_1, task_2]
while len(result_ids):
done_id, result_ids = ray.wait(result_ids, num_returns=1, timeout=None)
if done_id[0] is task_1:
print('task one completed')
elif done_id[1] is task_2:
print('task_2 is completed')
print('COMPLETED TASKS')
Exception Occurred: 'object_refs' must either be an ObjectRef or a list of ObjectRefs. error is getting due to the code.

Related

Loop is not being called but indentation is correct

I have this function I have built and it seems like I am missing something very minor here. This loop will not call for some reason. Indentation looks good and I am not getting any errors in my output, but the data is not being updated which you can see by the debug print statement I made not showing in the terminal.
Below is the function I am speaking of:
def updatefield(layer, prev_data):
print("DEUBG:: UPDATE FIELD")
fname, ftype, fdict = field_attr(prev_data)[0], field_attr(prev_data)[1], field_attr(prev_data)[2]
arcpy.AddField_management(
in_table=layer,
field_name=fname,
field_type=ftype,
field_is_nullable="NULLABLE"
)
# Use UpdateCursor to expedite the copy of the data to the new field.
with arcpy.da.UpdateCursor(layer, [prev_data, fname, "OID#"]) as uc:
print("DEUBG:: UPDATE CURSOR: {}".format(layer))
for record in uc:
print("\tDEBUG:: record: {}".format(record)) # THIS NEVER GETS CALLED
if record[0] in fdict.keys():
record[1] = fdict[record[0]]
else:
if layer == fms_conduit:
record[1] = None
# Can't recall if you can have empty keys...These also pretty much only apply to fiber.
elif prev_data == "CATEGORY" and record[0] == "":
record[1] = "OTH"
elif prev_data == "CABLECAPACITY" and (record[0] in ('', ' ', None)):
record[1] = 0
elif prev_data == "CABLECAPACITY":
record[1] = int(record[0])
else:
record[1] = ""
print("\nDEBUG:: OID: {}\tPrevField: {}\t NewFieldName: {}\tNewFieldValue: {}".format(
record[2], prev_data, fname, record[1])
)
uc.updateRow(record)
And here is the output:
The final few debug print statements are from another function but I should be getting that record printed. Again, probably something silly but I can't seem to get it.
Thank you for your time.
Turns out the data that was delivered to me was truncated at some point. The update cursor was empty and therefore nothing was being translated.

Granite ORM + fiber + mysql = Unexpected EOF (Exception)?

Is working with Fibers in Granite possible? I didn't have a reason to believe otherwise, until I attempted to write some code for it. I am attempting to learn to work with Fibers, so perhaps incorrect fiber implementation could also be a cause. The following code results in an exception. Any hints to resolve this?
lines = File.read_lines INVENTORY_FILE
channel = Channel(Int32).new(199)
lines[0..200].each do |line|
row_number += 1
proc = ->(line : NamedTuple(item_number: String) do
spawn do
sku = Sku.find_by :item_number, item_number
channel.send(1)
end
end
proc.call({ line: line })
end
199.times do |i|
channel.receive
end
The above results in the following repeated exception starting from the very first fiber's attempt to access the DB. I've ensured that MySQL is in a working state and that the code works when fibers are not in use:
Unexpected EOF (Exception)
from lib/mysql/src/mysql/read_packet.cr:38:18 in 'read_byte!'
from lib/mysql/src/mysql/read_packet.cr:63:5 in 'read_int'
from lib/mysql/src/mysql/packets.cr:13:7 in 'read'
from lib/mysql/src/mysql/connection.cr:63:14 in 'read_packet'
from lib/mysql/src/mysql/connection.cr:22:19 in 'initialize'
from lib/mysql/src/mysql/connection.cr:4:3 in 'new'
from lib/mysql/src/mysql/driver.cr:3:5 in 'build_connection'
from lib/db/src/db/pool.cr:255:3 in 'build_resource'
from lib/db/src/db/pool.cr:34:22 in 'checkout'
from lib/db/src/db/pool.cr:65:7 in 'checkout_some'
from lib/db/src/db/database.cr:99:7 in 'checkout_some'
from lib/db/src/db/pool_prepared_statement.cr:35:24 in 'build_statement'
from lib/db/src/db/pool_prepared_statement.cr:53:22 in 'initialize'
from lib/db/src/db/pool_prepared_statement.cr:11:5 in 'new'
from lib/db/src/db/database.cr:89:7 in 'build_prepared_statement'
from lib/db/src/db/database.cr:7:15 in 'fetch_or_build_prepared_statement'
from lib/db/src/db/session_methods.cr:23:9 in 'build'
from lib/db/src/db/query_methods.cr:38:7 in 'query'
from lib/granite/src/granite/querying.cr:53:12 in 'find_by'
from /usr/share/crystal/src/kernel.cr:0:3 in '~procProc(Nil)'
from /usr/share/crystal/src/fiber.cr:255:3 in 'run'
from /usr/share/crystal/src/concurrent.cr:0:3 in '~proc2Proc(Fiber, (IO::FileDescriptor | Nil))'
from ???

implicitly_wait VS. time_sleep() in Selenium 3.0.2 Python 2.7

I'm trying to implement a simple sub dowloader with Selenium on Python 2.7 going on the http://www.yifysubtitles.com website.
I have run into serious troubles with implementing waits either implicit or explicit in Selenium - surely caused by ignorance rather than code.
I eventually opted for the destroy-all time.sleep() solution. The program works but I would like a more programatic solution and a prettier code.
Here is the code with the both implicit and explicit waits:
user_input = raw_input("Enter the movie of interest: ")
profile = SetProfile()
browser = webdriver.Firefox(profile)
# browser.implicitly_wait(30)
WindowSize(400, 400)
browser.get(url)
print "Searching..."
search_bar = browser.find_element_by_id("qSearch")
click_button = browser.find_element_by_class_name("glyphicon")
#wait_1 = WebDriverWait(browser, 50)
#cookie_element = wait_1.until(EC.element_to_be_clickable((By.CLASS_NAME,"cc_btn.cc_btn_accept_all")))
time.sleep(5)
accept_cookies_btn = browser.find_element_by_class_name("cc_btn.cc_btn_accept_all")
search_bar.clear()
accept_cookies_btn.click()
search_bar.send_keys(user_input)
search_bar.send_keys(Keys.RETURN)
time.sleep(10)
#wait_2 = WebDriverWait(browser, 50)
#result_element = wait_2.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "h3.media-heading")))
result = browser.find_element_by_class_name("media-heading")
result.click()
print "Movie found!"
time.sleep(10)
req = requests.get(browser.current_url)
soup = BeautifulSoup(req.text)
link_to_sub = ResultParser(soup)
print "Choosing the best eng. subtitle..."
browser.get(url+link_to_sub[1])
download_button = browser.find_element_by_class_name("btn-icon")
download_button.click()
time.sleep(10)
browser.quit()
print "Enjoy!"
I have commented out the waits (explicit or implicit) I tried to use. Indeed, If I use waits instead time.sleep, Selenium will throw at me an ElementNotVisibleException.
Thus, am I doing something wrong with the waits?
Regarding the implicit waits, Selenium always throws at me ElementNotVisibleException: Message: (With message empty) when I run the program, whatever the wait length (tried 5, 30 and 100!).
When I use explicit waits, similarly, Selenium either throw ElementNotVisibleException or seems to use the DOM from the preceding page. Indeed, in the last case the wait is after clicking on the result, but I get the heading of the previous page...
As I said, when I use time.sleep(), Selenium finds the elements without any trouble.
A long message for a simple question: why my waits seem to not work?

Django exception handling - catching ghost error from server-side

I have a Django view that is responsible for providing raw data for graph rendering on server-side. When I run the app locally (I have Windows 10 on my machine), everything works fine. But when I run the app from the remote server (it's Ubuntu), I get an 500-error. This type of errors (when identical code is ok locally, but fails remotely), currently constitutes a serious issue for me in that on every such occasion it leads to very time-consuming - and seemingly unprofessional - process to identify and fix them. And my question is, what is the magic exception handling code snippet which would help me through the detailed exception message to client-side console.
To be more specific, consider the following (I deliberately provide the code as is):
Django view:
def graph_vendors_black_white(request):
try:
legal_entity_own_qs = get_sale_points(request.user.id)
list = []
data = [x.blacknwhite_agreement_count(True) for x in legal_entity_own_qs]
list.append({'name':u'В белую', 'data':data});
data = [x.blacknwhite_agreement_count(False) for x in legal_entity_own_qs]
list.append({'name':u'В чёрную', 'data':data});
data = [x.blacknwhite_agreement_count(None) for x in legal_entity_own_qs]
list.append({'name':u'Не выбрана опция', 'data':data});
legal_entity_own_list = [unicode(x.short_identifier_rus) for x in legal_entity_own_qs]
json_response = JsonResponse({'time_series':json.dumps(list, ensure_ascii=False).encode('utf8'),'categories':json.dumps(legal_entity_own_list, ensure_ascii=False).encode('utf8')})
return json_response
except:
user_msg, tech_msg = default_error_msg()
return JsonResponse(user_msg + '\n ' + tech_msg, status = 500, safe = False)
def default_error_msg():
user_msg = u'Что-то пошло не так'
exc_type, exc_obj, exc_tb = sys.exc_info()
fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
try:
console_msg = exc_obj[1]
except:
console_msg = str(exc_obj.message)
tech_msg = ('File - ' + fname + ' on line ' + str(exc_tb.tb_lineno) + '. \n' + \
'Error message - ' + console_msg + '\n' + \
'Exception type - ' + str(type(exc_obj)))
return user_msg, tech_msg
JS:
$.get('/graph_vendors_black_white/', function (response)
{
// ... some code
}).fail(function(json_result) {
$('#black_white_container').html(json_result.responseJSON)
});
When I run this locally, it's perfectly ok. From remote server, it throws 500 error and most surprisingly json_result does not contain error message. Whereas if I put something like a = 1/0, I get the error message in the container instead of graph.
To sum up, the question is, what is the generic exception handling code snippet which would transmit the detailed error message to the fail-function response parameter NO MATTER WHAT THE ERROR IS on the server-side

Abort user request with Node.js/formidable

I'm using formidable to receive a file upload with node.js. I send some fields together with a file in a multipart request.
As soon as certain fields arrived, I'm able to validate the authenticity of the request for instance, and I would like to abort the whole request if this is not correct to avoid waisting resources.
I have not found a right way to abort the incoming request. I tried to use req.connection.destroy(); as follow:
form
.on('field', function(field, value) {
fields[field] = value;
if (!fields['token'] || !fields['id'] || !fields['timestamp']) {
return;
}
if (!validateToken(fields['token'], fields['id'], fields['timestamp'])) {
res.writeHead(401, {'Content-Type' : 'text/plain' });
res.end('Unauthorized');
req.connection.destroy();
}
})
However, this triggers the following error:
events.js:45
throw arguments[1]; // Unhandled 'error' event
^
Error: Cannot resume() closed Socket.
at Socket.resume (net.js:764:11)
at IncomingMessage.resume (http.js:254:15)
at IncomingForm.resume (node_modules/formidable/lib/incoming_form.js:52:11)
at node_modules/formidable/lib/incoming_form.js:181:12
at node_modules/formidable/lib/file.js:51:5
at fs.js:1048:7
at wrapper (fs.js:295:17)
I also tried req.connection.end() but the file keeps uploading.
Any thoughts? Thanks in advance!
The problem is that formidable didn't understand that you want it to stop. Try this:
req.connection.destroy();
req.connection.resume = function(){};
Of course, this is a somewhat ugly workaround, I'd open an issue on github.