AttributeError: module 'apscheduler.schedulers.asyncio' has no attribute 'get_event_loop' - python

I am trying to implement clock process to use in Heroku dyno. I am using Python 3.6. Clock process will run each 3 hours. This is the code:
import os
import sys
import requests
from apscheduler.schedulers import asyncio
from apscheduler.schedulers.asyncio import AsyncIOScheduler
from apscheduler.triggers.interval import IntervalTrigger
from webdriverdownloader import GeckoDriverDownloader
from scraper.common import main
def get_driver():
return True
def notify(str):
return True;
if __name__ == '__main__':
scheduler = AsyncIOScheduler()
get_driver()
scheduler.add_job(main, trigger=IntervalTrigger(hours=3))
scheduler.start()
# Execution will block here until Ctrl+C (Ctrl+Break on Windows) is pressed.
try:
loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.wait())
except (KeyboardInterrupt, SystemExit):
pass
At first I tried with
asyncio.get_event_loop().run_forever()
However, I read that this is not supported in python 3.6, so I changed this statement to run_until_complete.
If I run this example, the code prints out:
AttributeError: module 'apscheduler.schedulers.asyncio' has no attribute 'get_event_loop'
Does anyone know why this error occurs? Any help would be much appreciated. Thanks in advance!

You're not importing the asyncio module from the standard library but the asyncio module in the apscheduler library. You can see that by visiting the link here.
There are only two things you can import from that namespace:
run_in_event_loop
AsyncIOScheduler
If you need to use the low-level asyncio API just import asyncio directly from the standard library.

Related

Error 409 occurred in telebot when using multiprocessing [exe file]

I want to start another process, it will start, if I write to telegram bot "start". But right after this occurring the ERROR:
2022-04-11 11:16:13,602 (__init__.py:688 Thread-1)
ERROR - TeleBot: "A request to the Telegram API was unsuccessful. Error code: 409.
Description: Conflict: terminated by other getUpdates request;
make sure that only one bot instance is running"
I am running ONLY one bot at the same time, already checked it. I found, that the error occurs when program trying to start a new process. Could anyone help me, please?
from time import sleep
from datetime import datetime, timedelta
import telebot
from telebot import types
from threading import Thread
import multiprocessing
def collecting(a):
while True:
print(a)
sleep(3)
if __name__=='__main__':
bot = telebot.TeleBot(token)
workers = []
def listen():
#bot.message_handler(content_types='text')
def get_text(message):
id = message.chat.id
mes = message.text
if mes=='start':
bot.send_message(id, 'okey')
workers.append([str(datetime.now())])
else:
bot.send_message(id, mes)
bot.polling()
Thread(target=listen).start()
while True:
sleep(1)
x = workers[:]
for i in range(len(x)):
multiprocessing.Process(target=collecting, args=(x[i],)).start()
del workers[i]
UPD: Forgot to say, this error occurred only when I create exe file and run this code like exe program. This is how I create exe file:
pyinstaller --hidden-import=pkg_resources.py2_warn --onefile Program.py
Solved, I forgot to add this:
from multiprocessing import freeze_support
And then after if__name__=="__main__": also need to write freeze_support().

NameError: name 'threading' is not defined will running Python Program in Terminal

I have imported the following modules in my python file:-
from socket import *
from threading import *
But, I am still getting this error:-
NameError: name 'threading' is not defined
It is showing the error on this line:-
receive_thread = threading.Thread(target=receive)
Could anyone please let me know how could I resolve this issue? Any help would be highly appreciated! Thanks:)
If you import from threading import * you have to access the methods directly, without calling the class threading before. So you just import every method from threading and just use them:
from threading import *
receive_thread = Thread(target=receive)
otherwise import and then refer to the module itself
import threading
receive_thread = threading.Thread(target=receive)
import socket programming library
import socket
# import thread module
from _thread import *
import threading
print_lock = threading.Lock()

Update variable from imported module with Python when using Threading

In a project, I would like to separate the visualization and calculation in two different modules. The goal is to transfer the variables of the calculation-module to a main-script, in order to visualize it with the visualization-script.
Following this post
Using global variables between files?,
I am able to use a config-script in order to transfer a variable between to scripts now. But unfortunately, this is not working when using threading. The Output of the main.py is always "get: 1".
Does anyone have an idea?
main.py:
from threading import Thread
from time import sleep
import viz
import change
add_Thread = Thread(target=change.add)
add_Thread.start()
viz.py:
import config
from time import sleep
while True:
config.init()
print("get:", config.x)
sleep(1)
config.py:
x = 1
def init():
global x
change.py:
import config
def add():
while True:
config.x += 1
config.init()
OK, fount the answer by myself. Problem was in the "main.py". One has to put the "import viz" after starting the thread:
from threading import Thread
from time import sleep
import change
add_Thread = Thread(target=change.add)
add_Thread.start()
import viz

Allow Greenlet to finish work at end of main module execution

I'm making a library that uses gevent to do some work asynchronously. I'd like to guarantee that the work is completed, even if the main module finishes execution.
class separate_library(object):
def __init__(self):
import gevent.monkey; gevent.monkey.patch_all()
def do_work(self):
from gevent import spawn
spawn(self._do)
def _do(self):
from gevent import sleep
sleep(1)
print 'Done!'
if __name__ == '__main__':
lib = separate_library()
lib.do_work()
If you run this, you'll notice the program ends immediately, and Done! doesn't get printed.
Now, the main module doesn't know, or care, how separate_library actually accomplishes the work (or even that gevent is being used), so it's unreasonable to require joining there.
Is there any way separate_library can detect certain types of program exits, and stall until the work is done? Keyboard interrupts, SIGINTs, and sys.exit() should end the program immediately, as that is probably the expected behaviour.
Thanks!
Try using a new thread that is not a daemon thread that spawns your gevent threads. Your program will not exit due to this non daemon thread.
import gevent
import threading
class separate_library(object):
def __init__(self):
import gevent.monkey; gevent.monkey.patch_all()
def do_work(self):
t = threading.Thread(target=self.spawn_gthreads)
t.setDaemon(False)
t.start()
def spawn_gthreads(self):
from gevent import spawn
gthreads = [spawn(self._do,x) for x in range(10)]
gevent.joinall(gthreads)
def _do(self,sec):
from gevent import sleep
sleep(sec)
print 'Done!'
if __name__ == '__main__':
lib = separate_library()
lib.do_work()

Using Queue in python

I'm trying to run the following in Eclipse (using PyDev) and I keep getting error :
q = queue.Queue(maxsize=0)
NameError: global name 'queue' is not defined
I've checked the documentations and appears that is how its supposed to be placed. Am I missing something here? Is it how PyDev works? or missing something in the code? Thanks for all help.
from queue import *
def worker():
while True:
item = q.get()
do_work(item)
q.task_done()
def main():
q = queue.Queue(maxsize=0)
for i in range(num_worker_threads):
t = Thread(target=worker)
t.daemon = True
t.start()
for item in source():
q.put(item)
q.join() # block until all tasks are done
main()
Using:
Eclipse SDK
Version: 3.8.1
Build id: M20120914-1540
and Python 3.3
You do
from queue import *
This imports all the classes from the queue module already. Change that line to
q = Queue(maxsize=0)
CAREFUL: "Wildcard imports (from import *) should be avoided, as they make it unclear which names are present in the namespace, confusing both readers and many automated tools". (Python PEP-8)
As an alternative, one could use:
from queue import Queue
q = Queue(maxsize=0)
That's because you're using : from queue import *
and then you're trying to use :
queue.Queue(maxsize=0)
remove the queue part, because from queue import * imports all the attributes to the current namespace. :
Queue(maxsize=0)
or use import queue instead of from queue import *.
If you import from queue import * this is mean that all classes and functions importing in you code fully. So you must not write name of the module, just q = Queue(maxsize=100). But if you want use classes with name of module: q = queue.Queue(maxsize=100) you mast write another import string: import queue, this is mean that you import all module with all functions not only all functions that in first case.
make sure your code is not under queue.py rename it to something else.
if your file name is queue.py it will try to search in the same file.
You Can install kombu with
pip install kombu
and then Import queue Just like this
from kombu import Queue

Categories

Resources