In this video: https://www.youtube.com/watch?v=HGOBQPFzWKo, around 4:16:00, it's talked about how to implement a lock for threading issues. But as opposed to the video, I get a debug message when trying to acquire the lock.
This is a part of the code I tried out for myself:
def increase(lock):
global database_value
lock.acquire()
local_copy = database_value
local_copy += 1
time.sleep(0.1)
database_value = local_copy
lock.release()
And this is the debug message:
Exception in thread Thread-32:
Traceback (most recent call last):
File "C:\Users\Gebruiker\miniconda3\lib\threading.py", line 973, in _bootstrap_inner
Exception in thread Thread-33:
Traceback (most recent call last):
File "C:\Users\Gebruiker\miniconda3\lib\threading.py", line 973, in _bootstrap_inner
self.run()
File "C:\Users\Gebruiker\miniconda3\lib\threading.py", line 910, in run
self.run()
File "C:\Users\Gebruiker\miniconda3\lib\threading.py", line 910, in run
self._target(*self._args, **self._kwargs)
File "C:\Users\Gebruiker\AppData\Local\Temp\ipykernel_75580\1082204269.py", line 14, in increase
self._target(*self._args, **self._kwargs)
File "C:\Users\Gebruiker\AppData\Local\Temp\ipykernel_75580\1082204269.py", line 14, in increase
AttributeError: 'builtin_function_or_method' object has no attribute 'acquire'
AttributeError: 'builtin_function_or_method' object has no attribute 'acquire'
In the Jupyter Notebook, there seems to be raised an exception when I try to run the thread.
What's going wrong here?
Thanks in advance!
I tried to do what the video did, I expected it to work, but there's something not working in the thread apparently.
Related
I am new to vpython and trying to learn. However, right after installing the module with "pip install vpython" and making a python file that contains this :
from vpython import *
box()
I am greeted with an error:
Traceback (most recent call last):
File "C:/Users/remis/Downloads/Test Vpython.py", line 2, in <module>
box()
File "C:\Users\remis\AppData\Local\Programs\Python\Python38\lib\site-packages\vpython\vpython.py", line 1099, in __init__
super(box, self).setup(args)
File "C:\Users\remis\AppData\Local\Programs\Python\Python38\lib\site-packages\vpython\vpython.py", line 579, in setup
super(standardAttributes, self).__init__()
File "C:\Users\remis\AppData\Local\Programs\Python\Python38\lib\site-packages\vpython\vpython.py", line 236, in __init__
from .no_notebook import _
File "C:\Users\remis\AppData\Local\Programs\Python\Python38\lib\site-packages\vpython\no_notebook.py", line 271, in <module>
__w = threading.Thread(target=__server.serve_forever)
NameError: name '__server' is not defined
Exception ignored in: <function standardAttributes.__del__ at 0x00000182B9D4EAF0>
Traceback (most recent call last):
File "C:\Users\remis\AppData\Local\Programs\Python\Python38\lib\site-packages\vpython\vpython.py", line 1092, in __del__
super(standardAttributes, self).__del__()
File "C:\Users\remis\AppData\Local\Programs\Python\Python38\lib\site-packages\vpython\vpython.py", line 317, in __del__
cmd = {"cmd": "delete", "idx": self.idx}
AttributeError: 'box' object has no attribute 'idx'
I don't see any help online, can I get some please?
I have been automating some, and got a problem when migrating from 2.7 to 3.7. The code is as shown
def writeDataToOutput(dataOutput):
global myUartPort
global mySocket
if (m_UseRTT):
try:
mySocket.sendall(dataOutput)
except socket.error:
print('Send failed')
else:
myUartPort.write(dataOutput)
And when running i get this error message
Traceback (most recent call last):
File "monkeytest.py", line 1324, in <module>
executeScript()
File "monkeytest.py", line 1255, in executeScript
if executeCommandLine(line) == 0:
File "monkeytest.py", line 1159, in executeCommandLine
executeCommand(commandPayload, 1, 1)
File "monkeytest.py", line 587, in executeCommand
writeDataToOutput(command + "\r\n")
File "monkeytest.py", line 309, in writeDataToOutput
mySocket.sendall(dataOutput)
TypeError: a bytes-like object is required, not 'str'
I cannot see the wrong in my method, I have tried sendto() without any success.
I am trying to create two threads and set up a pipe to communicate between the threads. It is giving an error: 'TypeError: 'str' object is not callable'
I saw that the most common problem was that the arguments were not being passed as a tuple online but I fixed it. It's still throwing the same error.
# Create two threads and communicate between them using pipes
import os
#import random
#import string
import time
import threading
def task_for_thread1(r,w):
os.close(r)
write_obj=os.fdopen(w,'w')
for _ in range(1000):
char='a'
os.write(write_obj,'Thread 1 writes: '+char)
time.sleep(0.5)
write_obj.close()
def task_for_thread2(r,w):
os.close(w)
read_obj=os.fdopen(r)
while(True):
char_read=read_obj.read()
print('Received character',char_read,'from the pipe')
if __name__=='__main__':
print('Starting operation in main')
r,w=os.pipe()
thread1=threading.Thread(target='task_for_thread1',name='Thread1',args=(r,w,))
thread2=threading.Thread(target='task_for_thread2',name='Thread2',args=(r,w,))
thread1.start()
thread2.start()
thread1.join()
thread2.join()
And this is the output I get in the console.
Starting operation in main
18
19
Exception in thread Thread1:
Traceback (most recent call last):
File "C:\Users\vivek\Anaconda3\lib\threading.py", line 916, in _bootstrap_inner
self.run()
File "C:\Users\vivek\Anaconda3\lib\threading.py", line 864, in run
self._target(*self._args, **self._kwargs)
TypeError: 'str' object is not callable
Exception in thread Thread2:
Traceback (most recent call last):
File "C:\Users\vivek\Anaconda3\lib\threading.py", line 916, in _bootstrap_inner
self.run()
File "C:\Users\vivek\Anaconda3\lib\threading.py", line 864, in run
self._target(*self._args, **self._kwargs)
TypeError: 'str' object is not callable
I edited the code as per the comment.
# Create two threads and communicate between them using pipes
import os
#import random
#import string
import time
import threading
def task_for_thread1(r,w):
os.close(r)
for _ in range(1000):
char2='a'
os.write(w,char2)
time.sleep(0.5)
os.close(w)
def task_for_thread2(r,w):
os.close(w)
read_obj=os.fdopen(r)
while(True):
char_read=read_obj.read()
print('Received character',char_read,'from the pipe')
if __name__=='__main__':
print('Starting operation in main')
r,w=os.pipe()
print(r)
print(w)
thread1=threading.Thread(target=task_for_thread1,name='Thread1',args=(r,w,))
thread2=threading.Thread(target=task_for_thread2,name='Thread2',args=(r,w,))
thread1.start()
thread2.start()
thread1.join()
thread2.join()
But now I get the following error:
Starting operation in main
24
25
Exception in thread Thread1:
Traceback (most recent call last):
File "C:\Users\vivek\Anaconda3\lib\threading.py", line 916, in _bootstrap_inner
self.run()
File "C:\Users\vivek\Anaconda3\lib\threading.py", line 864, in run
self._target(*self._args, **self._kwargs)
File "E:/PythonScripts/multi_threading_pipe.py", line 11, in task_for_thread1
os.write(w,char2)
TypeError: a bytes-like object is required, not 'str'
Exception in thread Thread2:
Traceback (most recent call last):
File "C:\Users\vivek\Anaconda3\lib\threading.py", line 916, in _bootstrap_inner
self.run()
File "C:\Users\vivek\Anaconda3\lib\threading.py", line 864, in run
self._target(*self._args, **self._kwargs)
File "E:/PythonScripts/multi_threading_pipe.py", line 16, in task_for_thread2
read_obj=os.fdopen(r)
File "C:\Users\vivek\Anaconda3\lib\os.py", line 1015, in fdopen
return io.open(fd, *args, **kwargs)
OSError: [WinError 6] The handle is invalid
Anu pointers on what I may be doing wrong?
Since today I am experiencing some errors caused by the yahoo_finance package version 1.4.
Here is a code example that is causing the error:
from yahoo_finance import Share
Apple = Share("AAPL")
Results in the following Error:
Traceback (most recent call last):
File "C:\Users\Julian\Anaconda3\lib\site-packages\yahoo_finance\__init__.py", line 120, in _request
_, results = response['query']['results'].popitem()
AttributeError: 'NoneType' object has no attribute 'popitem'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\Julian\Anaconda3\lib\site-packages\yahoo_finance\__init__.py", line 123, in _request
raise YQLQueryError(response['error']['description'])
KeyError: 'error'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\Julian\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 2881, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-3-44fc1f59aa21>", line 1, in <module>
Apple = Share("AAPL")
File "C:\Users\Julian\Anaconda3\lib\site-packages\yahoo_finance\__init__.py", line 178, in __init__
self.refresh()
File "C:\Users\Julian\Anaconda3\lib\site-packages\yahoo_finance\__init__.py", line 142, in refresh
self.data_set = self._fetch()
File "C:\Users\Julian\Anaconda3\lib\site-packages\yahoo_finance\__init__.py", line 181, in _fetch
data = super(Share, self)._fetch()
File "C:\Users\Julian\Anaconda3\lib\site-packages\yahoo_finance\__init__.py", line 134, in _fetch
data = self._request(query)
File "C:\Users\Julian\Anaconda3\lib\site-packages\yahoo_finance\__init__.py", line 125, in _request
raise YQLResponseMalformedError()
yahoo_finance.YQLResponseMalformedError: Response malformed.
Do you experience similar issues or is this only an issue for me personally?
Thank you for your replies.
If yes - what are potential fixes for this issue?
I am trying to make a code that will ask for a user input and if that input is not given in a set amount of time will assume a default value and continue through the rest of the code without requiring the user to hit enter. I am running in Python 3.5.1 on Windows 10.
I have looked through: Keyboard input with timeout in Python, How to set time limit on raw_input, Timeout on a function call, and Python 3 Timed Input black boxing the answers but none of the answers are suitable as they are not usable on Windows (principally use of signal.SIGALRM which is only available on linux), or require a user to hit enter in order to exit the input.
Based upon the above answers however i have attempted to scrap together a solution using multiprocessing which (as i think it should work) creates one process to ask for the input and creates another process to terminate the first process after the timeout period.
import multiprocessing
from time import time,sleep
def wait(secs):
if secs == 0:
return
end = time()+secs
current = time()
while end>current:
current = time()
sleep(.1)
return
def delay_terminate_process(process,delay):
wait(delay)
process.terminate()
process.join()
def ask_input(prompt,term_queue,out_queue):
command = input(prompt)
process = term_queue.get()
process.terminate()
process.join()
out_queue.put(command)
##### this doesn't even remotly work.....
def input_with_timeout(prompt,timeout=15.0):
print(prompt)
astring = 'no input'
out_queue = multiprocessing.Queue()
term_queue = multiprocessing.Queue()
worker1 = multiprocessing.Process(target=ask_input,args=(prompt,term_queue,out_queue))
worker2 = multiprocessing.Process(target=delay_terminate_process,args=(worker1,timeout))
worker1.daemon = True
worker2.daemon = True
term_queue.put(worker2)
print('Through overhead')
if __name__ == '__main__':
print('I am in if statement')
worker2.start()
worker1.start()
astring = out_queue.get()
else:
print('I have no clue what happened that would cause this to print....')
return
print('returning')
return astring
please = input_with_timeout('Does this work?',timeout=10)
But this fails miserably and yields:
Does this work?
Through overhead
I am in if statement
Traceback (most recent call last):
File "C:\Anaconda3\lib\multiprocessing\queues.py", line 241, in _feed
obj = ForkingPickler.dumps(obj)
File "C:\Anaconda3\lib\multiprocessing\reduction.py", line 50, in dumps
cls(buf, protocol).dump(obj)
File "C:\Anaconda3\lib\multiprocessing\queues.py", line 58, in __getstate__
context.assert_spawning(self)
File "C:\Anaconda3\lib\multiprocessing\context.py", line 347, in assert_spawning
' through inheritance' % type(obj).__name__
RuntimeError: Queue objects should only be shared between processes through inheritance
Does this work?
Through overhead
I have no clue what happened that would cause this to print....
Does this work?Process Process-1:
Traceback (most recent call last):
File "C:\Anaconda3\lib\multiprocessing\queues.py", line 241, in _feed
obj = ForkingPickler.dumps(obj)
File "C:\Anaconda3\lib\multiprocessing\reduction.py", line 50, in dumps
cls(buf, protocol).dump(obj)
File "C:\Anaconda3\lib\multiprocessing\process.py", line 287, in __reduce__
'Pickling an AuthenticationString object is '
TypeError: Pickling an AuthenticationString object is disallowed for security reasons
Traceback (most recent call last):
File "C:\Anaconda3\lib\multiprocessing\process.py", line 254, in _bootstrap
self.run()
File "C:\Anaconda3\lib\multiprocessing\process.py", line 93, in run
self._target(*self._args, **self._kwargs)
File "C:\Anaconda3\saved_programs\a_open_file4.py", line 20, in ask_input
command = input(prompt)
EOFError: EOF when reading a line
Does this work?
Through overhead
I have no clue what happened that would cause this to print....
Traceback (most recent call last):
File "C:\Anaconda3\lib\multiprocessing\queues.py", line 241, in _feed
obj = ForkingPickler.dumps(obj)
File "C:\Anaconda3\lib\multiprocessing\reduction.py", line 50, in dumps
cls(buf, protocol).dump(obj)
File "C:\Anaconda3\lib\multiprocessing\queues.py", line 58, in __getstate__
context.assert_spawning(self)
File "C:\Anaconda3\lib\multiprocessing\context.py", line 347, in assert_spawning
' through inheritance' % type(obj).__name__
RuntimeError: Queue objects should only be shared between processes through inheritance
Process Process-2:
Traceback (most recent call last):
File "C:\Anaconda3\lib\multiprocessing\process.py", line 254, in _bootstrap
self.run()
File "C:\Anaconda3\lib\multiprocessing\process.py", line 93, in run
self._target(*self._args, **self._kwargs)
File "C:\Anaconda3\saved_programs\a_open_file4.py", line 16, in delay_terminate_process
process.terminate()
File "C:\Anaconda3\lib\multiprocessing\process.py", line 113, in terminate
self._popen.terminate()
AttributeError: 'NoneType' object has no attribute 'terminate'
I really don't understand the multiprocessing module well and although I have read the official docs am unsure why this error occurred or why it appears to have ran through the function call 3 times in the process. Any help on how to either resolve the error or achieve an optional user input in a cleaner manner will be much appreciated by a noob programmer. Thanks!