Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #![feature(concat_idents,duration_as_u128)]
- extern crate pyo3;
- use std::thread;
- use std::time;
- use pyo3::prelude::*;
- use pyo3::ffi::*;
- fn make_datetime() -> Py<PyDateTime> {
- let gil = Python::acquire_gil();
- let py = gil.python();
- macro_rules! unwrap_py {
- ($e:expr) => { ($e).map_err(|e| e.print(py)).unwrap() }
- };
- let datetime = unwrap_py!(py.import("datetime"));
- let locals = PyDict::new(py);
- locals.set_item("timezone", datetime.get("timezone").unwrap())
- .unwrap();
- let UTC_ref = unwrap_py!(py.eval("timezone.utc", Some(locals), None));
- // .extract::<&'a PyTzInfo>().unwrap().to_object(py);
- let UTC = unsafe {
- &*(UTC_ref as *const PyObjectRef as *const pyo3::PyObject)
- };
- PyDateTime::new(py, 2011, 1, 1, 0, 0, 0, 0, Some(&UTC)).unwrap()
- }
- fn make_string() -> Py<PyString> {
- let gil = Python::acquire_gil();
- let py = gil.python();
- PyString::new(py, "Example")
- }
- fn main() {
- let now = time::Instant::now();
- let mut handles = Vec::new();
- for _thread in 0..2 {
- println!("Spawning thread {}", _thread);
- let handle = thread::spawn(move || {
- for _call in 0..2 {
- println!("Starting datetime call {} from thread {}", _thread, _call);
- make_datetime();
- // make_string();
- println!("Finished call {}", _call);
- }
- });
- handles.push(handle);
- }
- for handle in handles.into_iter() {
- handle.join().unwrap();
- }
- println!("Done in {}us", now.elapsed().as_micros())
- }
Add Comment
Please, Sign In to add comment